![](https://i-blog.csdnimg.cn/blog_migrate/33a10da1524a61bc9b3561b209134e5e.png)
1)async / await
2)对象/数组/集合初始化器
//一些演示类public class Employee { public string Name {get; set;} public DateTime StartDate {get; set;}}//使用初始化器创建employee Employee emp = new Employee {Name="John Smith", StartDate=DateTime.Now()};
3)Lambdas,谓词,delegates和闭包
4)??(空合并运算符)
//可能为nullvar someValue = service.GetValue();var defaultValue = 23//如果someValue为null,结果将为23var result = someValue ?? defaultValue;
string anybody = parm1 ?? localDefault ?? globalDefault;
var totalPurchased = PurchaseQuantities.Sum(kvp => kvp.Value ?? 0);
5)$“{x}”(字符串插值) ——C#6
//旧方法var someString = String.Format("Some data: {0}, some more data: {1}", someVariable, someOtherVariable);//新方法var someString = $"Some data: {someVariable}, some more data: {someOtherVariable}";
6)?.(Null条件运算符) ——C#6
//Null if customer or customer.profile or customer.profile.age is nullvar currentAge = customer?.profile?.age;
7)nameof Expression ——C#6
public void PrintUserName(User currentUser){ //The refactoring tool might miss the textual reference to current user below if we're renaming it if(currentUser == null) _logger.Error("Argument currentUser is not provided"); //...}
public void PrintUserName(User currentUser){ //The refactoring tool will not miss this... if(currentUser == null) _logger.Error($"Argument {nameof(currentUser)} is not provided"); //...}
8)属性初始化器 ——C#6
public class User{ public Guid Id { get; } = Guid.NewGuid(); // ...}
9)as和is 运算符
if (Person is Adult){ //do stuff}
SomeType y = x as SomeType;if (y != null){ //do stuff}10)yield 关键字
public static IEnumerable Power(int number, int exponent){ int result = 1; for (int i = 0; i < exponent; i++) { result = result * number; yield return result; }}