1.自动属性增强功能
例如:只读自动属性实现真正的只读行为。 你声明仅具有 get 访问器的自动属性:
new Code
public string FirstName { get; set; }
public string LastName { get; }
old Code
public string FirstName { get; private set; }
public string LastName { get; private set; }
2.Expression-bodied 函数成员
我们编写的很多成员的主体只包含一条可以表示为表达式的语句。 可通过改为编写 expression-bodied 成员来简化该语法。 它适用于方法和只读属性
例如,重写 ToString() 通常是理想之选:
new Code
public override string ToString() => $"{LastName}, {FirstName}";
old Code
public override string ToString()
{
return $"{LastName}, {FirstName}";
}
3.using static
用于导入单个类的静态方法 目标是尽可能减少现有基本代码
例如
using static System.String;
new Code
if (IsNullOrWhiteSpace(lastName))
old Code
if (string.IsNullOrWhiteSpace(lastName))
4.Null 条件运算符
Null 条件运算符使这些检查更轻松、更流畅
new Code
var first = person?.FirstName;
old Code
if(person!=null)
{
var first = person.FirstName
}
5.字符串内插
new Code
public string FullName => $"{FirstName} {LastName}";
old Code
public string FullName => $"{FirstName} {LastName}";
更多例子请看 官方文档