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.

异步 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.