美食网站开发现状,wordpress 手机浏览,重庆市建设工程造价信息,韩国怎么出线1#xff09;async / await
使用async / await-pattern允许在执行阻塞操作时解除UI /当前线程的阻塞。async / await-pattern的工作原理是让代码继续执行#xff0c;即使在某些东西阻塞了执行#xff08;如Web请求#xff09;的情况下。
阅读更多有关async / await-patte…1async / await
使用async / await-pattern允许在执行阻塞操作时解除UI /当前线程的阻塞。async / await-pattern的工作原理是让代码继续执行即使在某些东西阻塞了执行如Web请求的情况下。
有关async / await-pattern的信息请访问https://msdn.microsoft.com/en-us/library/hh191443.aspx
2对象/数组/集合初始化器
通过使用对象、数组和集合初始化器可以轻松地创建类、数组和集合的实例
//一些演示类
public class Employee {public string Name {get; set;}public DateTime StartDate {get; set;}
}
//使用初始化器创建employee Employee emp new Employee {Name”John Smith”, StartDateDateTime.Now()}; 上面的例子在单元测试中才真正有用但在其他上下文中应该避免因为类的实例应该使用构造函数创建。
有关初始化器的信息请访问https://msdn.microsoft.com/en-us/library/bb384062.aspx
3Lambdas谓词delegates和闭包
在许多情况下例如使用Linq时这些功能实际上是必需的确保学习何时以及如何使用它们。
关于Lambdas谓词delegates和闭包的信息请访问http://www.codeaddiction.net/articles/13/lambda-expressions-delegates-predicates-and-closures-in-c
4??空合并运算符
?? – 运算符返回左侧只要它不为null那样的情况下返回 //可能为null var someValue service.GetValue(); var defaultValue 23
//如果someValue为null结果将为23 var result someValue ?? defaultValue; ?? – 运算符可以链接 ing anybody parm1 ?? localDefault ?? globalDefault; 并且它可以用于将可空类型转换为不可空
var totalPurchased PurchaseQuantities.Sum(kvp kvp.Value ?? 0); 有关?? – 运算符的信息请访问https://msdn.microsoft.com/en-us/library/ms173224.aspx
5$“{x}”字符串插值 ——C#6
这是C#6的一个新功能可以让你用高效和优雅的方式组装字符串 //旧方法 var someString String.Format(“Some data: {0}, some more data: {1}”, someVariable, someOtherVariable); //新方法 var someString $”Some data: {someVariable}, some more data: {someOtherVariable}”; 你可以把C#表达式放在花括号之间这使得此字符串插值非常强大。
6?.Null条件运算符 ——C#6
null条件运算符的工作方式如下 //Null if customer or customer.profile or customer.profile.age is null var currentAge customer?.profile?.age; 没有更多NullReferenceExceptions 有关?.-运算符的信息请访问https://msdn.microsoft.com/en-us/library/dn986595.aspx
7nameof Expression ——C#6
新出来的nameof-expression可能看起来不重要但它真的有它的价值。当使用自动重构因子工具如ReSharper时你有时需要通过名称引用方法参数 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”); //... } 你应该这样使用它…1public void PrintUserName(User currentUser) { //The refactoring tool will not miss this… if(currentUser null) _logger.Error($”Argument {nameof(currentUser)} is not provided”); //... }1有关nameof-expression的信息请访问https://msdn.microsoft.com/en-us/library/dn986596.aspx
8属性初始化器 ——C#6
属性初始化器允许你声明属性的初始值 public class User { public Guid Id { get; } Guid.NewGuid(); // … } 使用属性初始化器的一个好处是你不能声明一个集合嗯因此使得属性不可变。属性初始化器与C#6主要构造函数语法一起工作。
9as和is 运算符
is 运算符用于控制实例是否是特定类型例如如果你想看看是否可能转换 if (Person is Adult) { } 使用as运算符尝试将实例转换为类。如果不能转换它将返回null SomeType y x as SomeType; if (y ! null) { //do stuff } 10yield 关键字
yield 关键字允许提供带有条目的IEnumerable接口。 以下示例将返回每个2的幂幂指数从2到8例如2,4,8,16,32,64,128,256 public static IEnumerable Power(int number, int exponent) { int result 1; for (int i 0; i exponent; i) { result result * number; yield return result; } } yield返回可以非常强大如果它用于正确方式的话。 它使你能够懒惰地生成一系列对象即系统不必枚举整个集合——它就会按需完成。
11.?? ?: ?. ? 参考另一篇博文 译文链接http://www.codeceo.com/article/10-features-csharp-need-learn.html英文原文10 features in C# that you really should learn (and use!)翻译作者码农网 – 小峰 [ 转载必须在正文中标注并保留原文链接、译文链接和译者等信息。]