Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

A new feature in C# 6.0 allows to declare variable inside TryParse method. I have some code:

string s = "Hello";

if (int.TryParse(s, out var result))
{

}

But I receive compile errors: enter image description here

What I am doing wrong? P.S.: in project settings C# 6.0 and .NET framework 4.6 are set.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
318 views
Welcome To Ask or Share your Answers For Others

1 Answer

A new feature in C# 6.0 allows to declare variable inside TryParse method.

Declaration expressions was cut from C# 6.0 and wasn't shipped in the final release. You currently can't do that. There is a proposal for it on GitHub for C# 7 (also see this for future reference).

Update (07/03/2017)

With the official release of C#7, the following code compiles:

string s = "42";

if (int.TryParse(s, out var result))
{
     Console.WriteLine(result);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...