C# 7.0 语言功能在 .Net 上出现编译错误 4.x
C# 7.0 language features compile error on .Net 4.x
我有一个项目使用.Net 4.0.(目标框架是.Net 4)。
我使用Visual Studio2017编译源码,使用了如下代码:
if(int.TryParse(inputText, out int x))
StartAnotherMethod(x);
它在我的计算机上成功编译,所以我将它提交到存储库并等待构建服务器也编译。
令人惊讶的是,它有编译错误。
error CS1525: Invalid expression term 'int'
error CS1003: Syntax error, ',' expected
我在在线编译器中检查了它。它使用.Net 4.5,也显示编译错误。
https://dotnetfiddle.net/pibxC2
Compilation error (line 9, col 32): Invalid expression term 'int'
Compilation error (line 9, col 33): ; expected
Compilation error (line 9, col 33): Invalid expression term ')'
Compilation error (line 9, col 34): ; expected
Compilation error (line 9, col 34): Invalid expression term ')'
Compilation error (line 9, col 35): ; expected
据我所知,语言特性不需要新的框架,因此编译应该没有任何问题。
我错过了什么?
您没有使用 C# 7 的编译器。将编译器更改为 Roslyn。
这里的关键是可用的语言特性是由编译器决定的,而不是目标框架。
试试这个
int x;
if(int.TryParse(inputText, out x))
StartAnotherMethod(x);
这是构建服务器的编译问题。
我通过将一个语句分成两个来解决它,如下所示。
int.TryParse(输入,输出整数值);
解决方法:
整数值=0;
int.TryParse(输入,输出值);
或者如果允许,您可以在构建服务器上更改编译器。
我有一个项目使用.Net 4.0.(目标框架是.Net 4)。
我使用Visual Studio2017编译源码,使用了如下代码:
if(int.TryParse(inputText, out int x))
StartAnotherMethod(x);
它在我的计算机上成功编译,所以我将它提交到存储库并等待构建服务器也编译。
令人惊讶的是,它有编译错误。
error CS1525: Invalid expression term 'int'
error CS1003: Syntax error, ',' expected
我在在线编译器中检查了它。它使用.Net 4.5,也显示编译错误。
https://dotnetfiddle.net/pibxC2
Compilation error (line 9, col 32): Invalid expression term 'int'
Compilation error (line 9, col 33): ; expected
Compilation error (line 9, col 33): Invalid expression term ')'
Compilation error (line 9, col 34): ; expected
Compilation error (line 9, col 34): Invalid expression term ')'
Compilation error (line 9, col 35): ; expected
据我所知,语言特性不需要新的框架,因此编译应该没有任何问题。
我错过了什么?
您没有使用 C# 7 的编译器。将编译器更改为 Roslyn。
这里的关键是可用的语言特性是由编译器决定的,而不是目标框架。
试试这个
int x;
if(int.TryParse(inputText, out x))
StartAnotherMethod(x);
这是构建服务器的编译问题。 我通过将一个语句分成两个来解决它,如下所示。
int.TryParse(输入,输出整数值);
解决方法: 整数值=0; int.TryParse(输入,输出值);
或者如果允许,您可以在构建服务器上更改编译器。