C# 7.2 的新功能What's new in C# 7.2

C# 7.2 是另一個小數點版本,其中新增了一些實用的功能。C# 7.2 is another point release that adds a number of useful features. 此版本的主題之一是能夠避免不必要的複製或配置,讓實質型別作業能夠更有效率。One theme for this release is working more efficiently with value types by avoiding unnecessary copies or allocations.

其餘功能則無足輕重。The remaining features are small, nice-to-have features.

C# 7.2 使用了語言版本選取項目組態元素來選取編譯器語言版本。C# 7.2 uses the language version selection configuration element to select the compiler language version.

此版本的新款語言功能包括:The new language features in this release are:

此文章的其餘部分將概述各個功能。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.

安全、有效率的程式碼增強功能Safe efficient code enhancements

您可利用 7.2 版新加入的語言功能,於使用參考語意時運用實值型別。Language features introduced in 7.2 let you work with value types while using reference semantics. 這些功能之設計目的是加強加效能,方法是將複製實質型別最小化,而不產生與使用參考型別相關的記憶體配置。They are designed to increase performance by minimizing copying value types without incurring the memory allocations associated with using reference types. 功能包括:The features include:

  • 參數上的 in 修飾詞,可指定引數將以傳址方式傳遞,但不受呼叫的方法修改。The in modifier on parameters, to specify that an argument is passed by reference but not modified by the called method. in 修飾詞新增至引數是來源相容變更Adding the in modifier to an argument is a source compatible change.
  • 方法上 ref readonly 修飾詞的傳回內容,可指示方法要以傳址方式傳回其值,但不允許寫入該物件。The ref readonly modifier on method returns, to indicate that a method returns its value by reference but doesn't allow writes to that object. 如果將傳回項目指派給值。則新增 ref readonly 修飾詞是來源相容變更Adding the ref readonly modifier is a source compatible change, if the return is assigned to a value. readonly 修飾詞新增至現有 ref 傳回陳述式是不相容的變更Adding the readonly modifier to an existing ref return statement is an incompatible change. 需要呼叫端更新 ref 本機變數的宣告,以包含 readonly 修飾詞。It requires callers to update the declaration of ref local variables to include the readonly modifier.
  • readonly struct 宣告,可指示結構會固定,且應以 in 參數傳遞至其成員方法。The readonly struct declaration, to indicate that a struct is immutable and should be passed as an in parameter to its member methods. readonly 修飾詞新增至現有 struct 宣告是二進位相容變更Adding the readonly modifier to an existing struct declaration is a binary compatible change.
  • ref struct 宣告,可指示結構類型會直接存取受控記憶體,且一律會配置堆疊。The ref struct declaration, to indicate that a struct type accesses managed memory directly and must always be stack allocated. ref 修飾詞新增至現有 struct 宣告是不相容的變更Adding the ref modifier to an existing struct declaration is an incompatible change. ref struct 不能是類別的成員,或用於可能會在堆積上配置的其他位置。A ref struct cannot be a member of a class or used in other locations where it may be allocated on the heap.

您可以在撰寫安全、有效率的程式碼深入了解這些變更。You can read more about all these changes in Write safe efficient code.

非後置具名引數Non-trailing named arguments

當具名引數位於正確位置時,方法呼叫現在可以在位置引數前面使用具名引數。Method calls may now use named arguments that precede positional arguments when those named arguments are in the correct positions. 如需詳細資訊,請參閱具名和選擇性引數For more information see Named and optional arguments.

數值常值中的前置底線Leading underscores in numeric literals

C# 7.0 中的數字分隔符號之實作支援,並不允許將 _ 作為常值的第一個字元。The implementation of support for digit separators in C# 7.0 didn't allow the _ to be the first character of the literal value. 十六進位與二進位數值常值現在可以使用 _ 開頭。Hex and binary numeric literals may now begin with an _.

例如:For example:

int binaryValue = 0b_0101_0101;

private protected 存取修飾詞private protected access modifier

新的複合存取修飾詞:private protected 指出可藉由包含類別或在相同組件中宣告的衍生類別來存取成員。A new compound access modifier: private protected indicates that a member may be accessed by containing class or derived classes that are declared in the same assembly. protected internal 允許衍生類別或相同組件中的類別進行存取,而 private protected 則限制在相同組件中宣告的衍生類型進行存取。While protected internal allows access by derived classes or classes that are in the same assembly, private protected limits access to derived types declared in the same assembly.

如需詳細資訊,請參閱語言參考中的存取修飾詞For more information see access modifiers in the language reference.

ref 條件運算式Conditional ref expressions

最後,條件運算式可能會產生參考結果,而不是實值結果。Finally, the conditional expression may produce a ref result instead of a value result. 例如,您可以撰寫下列程式碼,在兩個陣列的其中一個上,擷取對第一個元素的參考:For example, you would write the following to retrieve a reference to the first element in one of two arrays:

ref var r = ref (arr != null ? ref arr[0] : ref otherArr[0]);

變數 rarrotherArr 中對第一個值的參考。The variable r is a reference to the first value in either arr or otherArr.

如需詳細資訊,請參閱語言參考中的條件運算子 (?:)For more information, see conditional operator (?:) in the language reference.