【发布时间】:2018-11-11 03:21:48
【问题描述】:
我正在使用下面的代码在 C# 7.0 中尝试新的元组语法,但它没有编译。 VS 2017 15.7 .Net 4.6 不会给出错误计数,但会在输出窗口中显示错误。 Resharper 没有指出任何错误。相同的代码在 Linqpad 中运行。我安装了Install-Package System.ValueTuple -Version 4.5.0。
我错过了什么?
private (bool a, bool b) Get(int num)
{
return (true, false);
}
错误是:
error CS1519: Invalid token '(' in class, struct, or interface member declaration
error CS1001: Identifier expected
error CS1002: ; expected
Syntax error, ',' expected
error CS1002: ; expected
error CS1520: Method must have a return type
error CS1026: ) expected
error CS1002: ; expected
error CS1513: } expected
error CS1002: ; expected
error CS1513: } expected
【问题讨论】:
-
愚蠢的问题 - 您是否检查了所有项目的构建设置以确认您使用的是最新的 C#? Resharper 的更新语言工具对我来说根本不起作用,所以如果你使用它,它可能也会搞砸你的解决方案。
-
当你说“在输出窗口中”——当你编译,或者当你运行?例如,如果此代码在 Razor 视图中,则这对于该问题可能非常重要。请提供更多上下文。 (如果您只是以正常方式编译,则不需要安装额外的 NuGet 包 - 如果在执行时完成任何编译,那是另一回事。)
-
如果你能提供一个minimal reproducible example 来证明这个问题,那就更好了——你能在一个小控制台应用程序中重现它吗?
-
产生的错误表明正在使用较旧的 C# 编译器(7.0 之前)。它根本不识别元组语法...
-
@JulienCouvreur:确实 - 这向我表明它是在执行时(或通过其他渠道)发生的编译。完全使用 VS2017 应该允许一般的元组代码,对吧? OP 尝试使用另一个 C# 7 功能来查看他们是否观察到相同的事情可能会很有趣。
标签: c# visual-studio-2017 tuples c#-7.0