如何在 Visual Studio 2015 中使用 C# 7?

How to use C# 7 with Visual Studio 2015?

Visual Studio 2017 (15.x) 支持 C# 7,但是 Visual Studio 2015 (14.x) 呢?

如何使用 C# 7?

您可以通过安装 Nuget 包 Microsoft.Net.Compilers:

将 Visual Studio 随附的编译器替换为支持 C# 7 的版本

Referencing this package will cause the project to be built using the specific version of the C# and Visual Basic compilers contained in the package, as opposed to any system installed version.

没有迹象表明我可以在 Visual Studio 2015 年官方支持的软件包页面上看到它。到目前为止,我的不彻底测试表明它可以工作但并非无痛 - C# 7 代码编译,但带有红色波浪线下划线,表示语法错误: Note that you will also need to install the Nuget package System.ValueTuple 使用新的 C# 7 值元组功能。

就我而言,只安装 Microsoft.Net.Compilers 是行不通的。相反,我必须执行以下操作:

  1. 为项目安装Microsoft.CodeDom.Providers.DotNetCompilerPlatform工具 => NuGet 包管理器 => 管理解决方案的 Nuget 包...) 并安装 Microsoft.CodeDom.Providers.DotNetCompilerPlatform.
  2. 为项目
  3. 安装最新的Microsoft.Net.Compilers
  4. 为您要使用的最新 C# 功能安装任何其他 NuGet 包。就我而言,我的目标是使用元组,所以我安装了 System.ValueTuple 并且运行良好。

但仍请注意,Visual Studio 2015 默认编译器无法识别的 C# 代码下方仍会有红色波浪线。