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.

非同步主要Async main

「非同步主要」 方法可讓您在 Main 方法中使用 awaitAn 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();
}

您可以在程式設計指南的非同步主要文章中閱讀更多詳細資料。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;

如需詳細資訊,請參閱預設運算子一文中的預設常值一節。For more information, see the default literal section of the default operator article.

推斷的 Tuple 元素名稱Inferred tuple element names

本方法為 C# 7.0 版本 Tuple 方法的改進,This feature is a small enhancement to the tuples feature introduced in C# 7.0. 許多時候,當您初始化 Tuple 時,用於指派右側的變數會與您想要的元組項目名稱相同: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 型別的型別,以及您想要避免 boxing 時,這可能非常有用。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.