TryParse with out var param

后端 未结 3 394
臣服心动
臣服心动 2020-12-09 00:19

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))
{

}         


        
3条回答
  •  不知归路
    2020-12-09 00:58

    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);
    }
    

提交回复
热议问题