TryParse没有var参数 [英] TryParse with out var param

查看:47
本文介绍了TryParse没有var参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C#6.0中的一项新功能允许在TryParse方法内声明变量。
我有一些代码:

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:

我做错了什么?
PS:在项目设置中设置了C#6.0和.NET Framework 4.6。

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

推荐答案


C#6.0中的一项新功能允许在TryParse
方法内声明变量。

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

声明表达式是从C#6.0和尚未在最终版本中发布。您目前无法这样做。 在GitHub上有针对C#7的建议(另请参见供以后参考)。

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

随着C#7的正式发布,将编译以下代码:

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

string s = "42";

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

这篇关于TryParse没有var参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆