C# 7.1 中的新增功能What's new in C# 7.1

C# 7.1 是 C# 语言的第一个点版本(更新版本)。C# 7.1 is the first point release to the C# language. 它标志着该语言发布节奏的加速。It marks an increased release cadence for the language. 理想情况下,可以在每个新功能准备就绪时更快推出新功能。You can use the new features sooner, ideally when each new feature is ready. C# 7.1 增加了将编译器配置为匹配特定语言版本的功能。C# 7.1 adds the ability to configure the compiler to match a specified version of the language. 从而可以分别制定有关升级语言版本的决策和有关升级工具的决策。That enables you to separate the decision to upgrade tools from the decision to upgrade language versions.

C# 7.1 增加了语言版本选择配置元素、三个新的语言功能和新的编译器行为。C# 7.1 adds the language version selection configuration element, three new language features and new compiler behavior.

此版本中新增的语言功能包括:The new language features in this release are:

最后,编译器有 /refout/refonly 两个选项,可用于控制引用程序集生成Finally, the compiler has two options /refout and /refonly that control reference assembly generation.

语言版本选择Language version selection

自 Visual Studio 2017 版本 15.3 或 .NET Core SDK 2.0 起,C# 编译器支持 C# 7.1。The C# compiler supports C# 7.1 starting with Visual Studio 2017 version 15.3, or the .NET Core SDK 2.0. 但会默认关闭 7.1 功能。However, the 7.1 features are turned off by default. 若要启用 7.1 功能,需要更改项目的语言版本设置。To enable the 7.1 features, you need to change the language version setting for your project.

在 Visual Studio 中,右键单击解决方案资源管理器中的项目节点,然后选择属性In Visual Studio, right-click on the project node in Solution Explorer and select Properties. 选择生成选项卡并选择高级按钮。Select the Build tab and select the Advanced button. 如下图所示,在下拉列表中,选择 C# 最新次要版本(最新),或具体版本 C# 7.1In the dropdown, select C# latest minor version (latest), or the specific version C# 7.1 as shown in the image following. latest 值表示你想要在当前计算机上使用最新次版本。The latest value means you want to use the latest minor version on the current machine. C# 7.1 意味着你想要使用 C# 7.1,甚至在更新的次要版本发布之后。The C# 7.1 means that you want to use C# 7.1, even after newer minor versions are released.

设置语言版本

或者,您可以编辑 "csproj" 文件,添加或修改以下行:Alternatively, you can edit the "csproj" file and add or modify the following lines:

<PropertyGroup>
  <LangVersion>latest</LangVersion>
</PropertyGroup>

备注

如果使用 Visual Studio IDE 来更新 csproj 文件,IDE 将为每个生成配置创建单独的节点。If you use the Visual Studio IDE to update your csproj files, the IDE creates separate nodes for each build configuration. 通常在所有生成配置中都设置相同的值,但你需要为每个生成配置显式地设置值,或在修改此设置时选择"所有配置"。You'll typically set the value the same in all build configurations, but you need to set it explicitly for each build configuration, or select "All Configurations" when you modify this setting. 在 csproj 文件中,你可以看到如下内容:You'll see the following in your csproj file:

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
  <LangVersion>latest</LangVersion>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
  <LangVersion>latest</LangVersion>
</PropertyGroup>

LangVersion 元素的有效设置选项:Valid settings for the LangVersion element are:

  • ISO-1
  • ISO-2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 7.1
  • default
  • latest

特殊字符串 defaultlatest 分别解析为生成计算机上安装的最新主要和次要语言版本。The special strings default and latest resolve to the latest major and minor language versions installed on the build machine, respectively.

采用此设置后,在开发环境中安装新版本的 SDK 和工具时,不必选择在项目中引入新的语言功能。This setting decouples installing new versions of the SDK and tools in your development environment from choosing to incorporate new language features in a project. 可以在生成计算机上安装最新的 SDK 和工具。You can install the latest SDK and tools on your build machine. 每个项目可以配置为对其生成使用该语言的特定版本。Each project can be configured to use a specific version of the language for its build.

异步 main 方法

Async main

异步 Main 方法使你能够在 Main 方法中使用 await 关键字。An async main method enables you to use await in your Main method. 在过去,需要编写:Previously you would need to write:

static int Main()
{
    return DoAsyncWork().GetAwaiter().GetResult();
}

现在,您可以编写:You can now write:

static async Task<int> Main()
{
    // This could also be replaced with the body
    // DoAsyncWork, including its await expressions:
    return await DoAsyncWork();
}

如果程序不返回退出代码,可以声明返回 TaskMain 方法: If your program doesn't return an exit code, you can declare a Main method that returns a Task:

static async Task Main()
{
    await SomeAsyncMethod();
}

你可以在编程指南的异步 Main 方法主题中阅读更多详细信息。You can read more about the details in the async main topic in the programming guide.

默认文本表达式Default literal expressions

默认文本表达式是针对默认值表达式的一项增强功能。Default literal expressions are an enhancement to default value expressions. 这些表达式将变量初始化为默认值。These expressions initialize a variable to the default value. 过去会这么编写:Where you previously would write:

Func<string, bool> whereClause = default(Func<string, bool>);

现在,可以省略掉初始化右侧的类型:You can now omit the type on the right-hand side of the initialization:

Func<string, bool> whereClause = default;

你可以通过 C# 编程指南的默认值表达式 主题了解有关此增强功能的详细信息。 You can learn more about this enhancement in the C# Programming Guide topic on default value expressions.

此增强功能也会更改某些default 关键字 的分析规则。This enhancement also changes some of the parsing rules for the default keyword.

推断元组元素名称Inferred tuple element names

此功能是对 C# 7.0 中引入的元组功能一次小型增强。This feature is a small enhancement to the tuples feature introduced in C# 7.0. 在初始化元组时,许多时候,赋值操作右侧的变量名与用于元组元素的名称相同:Many times when you initialize a tuple, the variables used for the right side of the assignment are the same as the names you'd like for the tuple elements:

int count = 5;
string label = "Colors used in the map";
var pair = (count: count, label: label);

元组元素的名称可通过在 C# 7.1 中初始化元组时使用的变量进行推断:The names of tuple elements can be inferred from the variables used to initialize the tuple in C# 7.1:

int count = 5;
string label = "Colors used in the map";
var pair = (count, label); // element names are "count" and "label"

可以在元组主题中了解有关此功能的详细信息。You can learn more about this feature in the Tuples topic.

引用程序集生成Reference assembly generation

有两个新的编译器选项会生成“仅引用”程序集:/refout/refonlyThere are two new compiler options that generate reference-only assemblies: /refout and /refonly. 链接的主题详细介绍了这些选项和引用程序集。The linked topics explain these options and reference assemblies in more detail.