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:
- 编写安全高效代码的技巧Techniques for writing safe efficient code
- 结合了多项语法改进,可使用引用语义处理值类型。A combination of syntax improvements that enable working with value types using reference semantics.
- 非尾随命名参数Non-trailing named arguments
- 命名的参数可后接位置参数。Named arguments can be followed by positional arguments.
- 数值文字中的前导下划线Leading underscores in numeric literals
- 数值文字现可在任何打印数字前放置前导下划线。Numeric literals can now have leading underscores before any printed digits.
private protected
访问修饰符private protected
access modifierprivate protected
访问修饰符允许访问同一程序集中的派生类。Theprivate protected
access modifier enables access for derived classes in the same assembly.
- 条件
ref
表达式Conditionalref
expressions- 现在可以引用条件表达式 (
?:
) 的结果。The result of a conditional expression (?:
) can now be a reference.
- 现在可以引用条件表达式 (
本文的其余部分概述了每个功能。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:
- 安装 dotnet-try 全局工具。Install the dotnet-try global tool.
- 克隆 dotnet/try-samples 存储库。Clone the dotnet/try-samples repository.
- 将当前目录设置为 try-samples 存储库的 csharp7 子目录 。Set the current directory to the csharp7 subdirectory for the try-samples repository.
- 运行
dotnet try
。Rundotnet 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
修饰符,指定形参通过引用传递,但不通过调用方法修改。Thein
modifier on parameters, to specify that an argument is passed by reference but not modified by the called method. 将in
修饰符添加到参数是源兼容的更改。Adding thein
modifier to an argument is a source compatible change. - 针对方法返回的
ref readonly
修饰符,指示方法通过引用返回其值,但不允许写入该对象。Theref 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 theref readonly
modifier is a source compatible change, if the return is assigned to a value. 将readonly
修饰符添加到现有的ref
返回语句是不兼容的更改。Adding thereadonly
modifier to an existingref
return statement is an incompatible change. 它要求调用方更新ref
本地变量的声明以包含readonly
修饰符。It requires callers to update the declaration ofref
local variables to include thereadonly
modifier. readonly struct
声明,指示结构不可变,且应作为in
参数传递到其成员方法。Thereadonly struct
declaration, to indicate that a struct is immutable and should be passed as anin
parameter to its member methods. 将readonly
修饰符添加到现有的结构声明是二进制兼容的更改。Adding thereadonly
modifier to an existing struct declaration is a binary compatible change.ref struct
声明,指示结构类型直接访问托管的内存,且必须始终分配有堆栈。Theref struct
declaration, to indicate that a struct type accesses managed memory directly and must always be stack allocated. 将ref
修饰符添加到现有struct
声明是不兼容的更改。Adding theref
modifier to an existingstruct
declaration is an incompatible change.ref struct
不能是类的成员,也不能用于可能在堆上分配的其他位置。Aref 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
最后,条件表达式可能生成 ref 结果而不是值。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]);
变量 r
是对 arr
或 otherArr
中第一个值的引用。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.
反馈
正在加载反馈...