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.

若要使用单点版本中的最新功能,需要配置编译器语言版本并选择版本。To use the latest features in a point release, you need to configure the compiler language version and select the version.

本文的其余部分概述了每个功能。The remainder of this article provides an overview of each feature. 你将了解每项功能背后的原理。For each feature, you'll learn the reasoning behind it. 将了解语法。You'll learn the syntax. 可以使用 dotnet try 全局工具在环境中浏览这些功能:You can explore these features in your environment using the dotnet try global tool:

  1. 安装 dotnet-try 全局工具。Install the dotnet-try global tool.
  2. 克隆 dotnet/try-samples 存储库。Clone the dotnet/try-samples repository.
  3. 将当前目录设置为 try-samples 存储库的 csharp7 子目录 。Set the current directory to the csharp7 subdirectory for the try-samples repository.
  4. 运行 dotnet tryRun dotnet try.

异步 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 article 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;

有关详细信息,请参阅 default 运算符一文中的 default 文本部分。For more information, see the default literal section of the default operator article.

推断元组元素名称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 article.

泛型类型参数的模式匹配Pattern matching on generic type parameters

自 C# 7.1 起,isswitch 类型模式的模式表达式的类型可能为泛型类型参数。Beginning with C# 7.1, the pattern expression for is and the switch type pattern may have the type of a generic type parameter. 这可能在检查 structclass 类型且要避免装箱时最有用。This can be most useful when checking types that may be either struct or class types, and you want to avoid boxing.

引用程序集生成Reference assembly generation

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