TryParse with out var param
Asked Answered
T

3

106

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.

Trundle answered 30/7, 2015 at 12:45 Comment(0)
F
172

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);
}
Foodstuff answered 30/7, 2015 at 12:46 Comment(6)
Thanks a lot for information! It's a pitty that it was cut from the final release, as for me it's a nice feature.Trundle
@Trundle Let's hope it gets into C# 7 then :)Foodstuff
That was most expect feature for me.Phebephedra
It is in C#7 / VS2017,at least.Oakes
Honestly, no. I didn't get past "There is a proposal for it for C# 7". If you're going to update the answer, it's probably best to get rid of that since it's well past proposal status at this point.Oakes
You could not read on, literally, one more sentence? The answer, which does state C# 7, also explains that it was cut from 6.0. People still develop in C# 6.0Spree
E
22

Just found out by accident, in vs2017, you can do this for brevity:

if (!Int64.TryParse(id, out _)) {
   // error or whatever...
}
Exuberate answered 24/9, 2017 at 12:21 Comment(1)
That is the discard operator.Pinkie
P
12

That is a new feature from C# 7, which is a very nice feature often used in conjunction with pattern matching. This feature, and many more, are announced in the C# team blog What’s New in C# 7.0.

The thing the team tries to achieve here is more fluid code. Do you remember some cases where the list of out variables is getting extremely long for no use? Just a quick example:

int i;
Guid g;
DateTime d;
if (int.TryParse(o, out i)) { /*use i*/ }
else if (Guid.TryParse(o, out g)) { /*use g*/ }
else if (DateTime.TryParse(o, out d)) { /*use d*/ }

See the problem? It is useless to have all those out variables sitting there doing nothing. The number of lines can be cut in half by using C# 7:

if (int.TryParse(o, out int i)) { /*use i*/ }
else if (Guid.TryParse(o, out Guid g)) { /*use g*/ }
else if (DateTime.TryParse(o, out DateTime d)) { /*use d*/ }

Not only the number of lines is minimized, there is also no unneccessary list of variables in scope where you don't want to have them. This prevents you to use a variable you didn't intend to use, but which is visible to you now.

This feature is also useful with pattern matching in switch statements, like in this code (which has a different behavior than the above code!):

switch (o)
{
    case int i: { /*use i*/ break; }
    case Guid g: { /*use g*/ break; }
    case DateTime d: { /*use d*/ break; }
}
Pinkie answered 10/5, 2017 at 8:20 Comment(1)
How does the switch statement work? Is it switching on the type of o or the value? I couldn't find anything about this way of using switch in the C# language reference page on selection statements.Quartis

© 2022 - 2024 — McMap. All rights reserved.