自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

C#中十個你真的應(yīng)該學習并投入使用的功能

開發(fā) 后端
如果你開始探索C#或決定擴展你的知識,那么你應(yīng)該學習這些有用的語言功能,這樣做有助于簡化代碼,避免錯誤,節(jié)省大量的時間。

如果你開始探索C#或決定擴展你的知識,那么你應(yīng)該學習這些有用的語言功能,這樣做有助于簡化代碼,避免錯誤,節(jié)省大量的時間。

[[179738]]

1)async / await

使用async / await-pattern允許在執(zhí)行阻塞操作時解除UI /當前線程的阻塞。async / await-pattern的工作原理是讓代碼繼續(xù)執(zhí)行,即使在某些東西阻塞了執(zhí)行(如Web請求)的情況下。

閱讀更多有關(guān)async / await-pattern的信息,請訪問:https://msdn.microsoft.com/en-us/library/hh191443.aspx

2)對象/數(shù)組/集合初始化器

通過使用對象、數(shù)組和集合初始化器,可以輕松地創(chuàng)建類、數(shù)組和集合的實例:

  1. //一些演示類 
  2. public class Employee { 
  3.     public string Name {get; set;} 
  4.     public DateTime StartDate {get; set;} 
  5. //使用初始化器創(chuàng)建employee  
  6. Employee emp = new Employee {Name="John Smith", StartDate=DateTime.Now()}; 

上面的例子在單元測試中才真正有用,但在其他上下文中應(yīng)該避免,因為類的實例應(yīng)該使用構(gòu)造函數(shù)創(chuàng)建。

閱讀更多有關(guān)初始化器的信息,請訪問:https://msdn.microsoft.com/en-us/library/bb384062.aspx

3)Lambdas,謂詞,delegates和閉包

在許多情況下(例如使用Linq時),這些功能實際上是必需的,確保學習何時以及如何使用它們。

閱讀更多關(guān)于Lambdas,謂詞,delegates和閉包的信息,請訪問:http://www.codeaddiction.net/articles/13/lambda-expressions-delegates-predicates-and-closures-in-c

4)(空合并運算符)

– 運算符返回左側(cè),只要它不為null;那樣的情況下返回右側(cè):

 

  1. //可能為null 
  2. var someValue = service.GetValue(); 
  3. var defaultValue = 23 
  4.  
  5. //如果someValue為null,結(jié)果將為23 
  6. var result = someValue ?? defaultValue; 

 – 運算符可以鏈接:

 

  1. string anybody = parm1 ?? localDefault ?? globalDefault; 

并且它可以用于將可空類型轉(zhuǎn)換為不可空:

 

  1. var totalPurchased = PurchaseQuantities.Sum(kvp => kvp.Value ?? 0); 

閱讀更多有關(guān)?? – 運算符的信息,請訪問:https://msdn.microsoft.com/en-us/library/ms173224.aspx

5)$“{x}”(字符串插值) ——C#6

這是C#6的一個新功能,可以讓你用高效和優(yōu)雅的方式組裝字符串:

 

  1. //舊方法 
  2. var someString = String.Format("Some data: {0}, some more data: {1}", someVariable, someOtherVariable); 
  3.  
  4. //新方法 
  5. var someString = $"Some data: {someVariable}, some more data: {someOtherVariable}"

你可以把C#表達式放在花括號之間,這使得此字符串插值非常強大。

6).(Null條件運算符) ——C#6

null條件運算符的工作方式如下:

 

  1. //Null if customer or customer.profile or customer.profile.age is null 
  2. var currentAge = customer?.profile?.age; 

沒有更多NullReferenceExceptions!

閱讀更多有關(guān)?.-運算符的信息,請訪問:https://msdn.microsoft.com/en-us/library/dn986595.aspx

7)nameof Expression ——C#6

新出來的nameof-expression可能看起來不重要,但它真的有它的價值。當使用自動重構(gòu)因子工具(如ReSharper)時,你有時需要通過名稱引用方法參數(shù):

 

  1. public void PrintUserName(User currentUser) 
  2.     //The refactoring tool might miss the textual reference to current user  
  3.     below if we're renaming it 
  4.     if(currentUser == null
  5.         _logger.Error("Argument currentUser is not provided"); 
  6.  
  7.     //... 

你應(yīng)該這樣使用它…

 

  1. public void PrintUserName(User currentUser) 
  2.     //The refactoring tool will not miss this... 
  3.     if(currentUser == null
  4.         _logger.Error($"Argument {nameof(currentUser)} is not provided"); 
  5.  
  6.     //... 

閱讀更多有關(guān)nameof-expression的信息,請訪問:https://msdn.microsoft.com/en-us/library/dn986596.aspx

8)屬性初始化器 ——C#6

屬性初始化器允許你聲明屬性的初始值:

  1. public class User 
  2.     public Guid Id { get; } = Guid.NewGuid(); 
  3.     // ... 

使用屬性初始化器的一個好處是你不能聲明一個集合:嗯,因此使得屬性不可變。屬性初始化器與C#6主要構(gòu)造函數(shù)語法一起工作。

9)as和is 運算符

is 運算符用于控制實例是否是特定類型,例如,如果你想看看是否可能轉(zhuǎn)換:

  1. if (Person is Adult) 
  2.     //do stuff 

使用as運算符嘗試將實例轉(zhuǎn)換為類。如果不能轉(zhuǎn)換,它將返回null:

  1. SomeType y = x as SomeType; 
  2. if (y != null
  3.     //do stuff 

10)yield 關(guān)鍵字

yield 關(guān)鍵字允許提供帶有條目的IEnumerable接口。 以下示例將返回每個2的冪,冪指數(shù)從2到8(例如,2,4,8,16,32,64,128,256):

  1. public static IEnumerable Power(int number, int exponent) 
  2.     int result = 1
  3.     for (int i = 0; i < exponent; i++) 
  4.     { 
  5.       result = result * number; 
  6.       yield return result; 
  7.     } 

yield返回可以非常強大,如果它用于正確方式的話。 它使你能夠懶惰地生成一系列對象,即,系統(tǒng)不必枚舉整個集合——它就會按需完成。

譯文鏈接:http://www.codeceo.com/article/10-features-csharp-need-learn.html
英文原文:10 features in C# that you really should learn (and use!)

責任編輯:張燕妮 來源: 碼農(nóng)網(wǎng)
相關(guān)推薦

2021-10-09 10:50:30

JavaScript編程開發(fā)

2013-12-09 09:20:17

谷歌GCEIaaS

2023-01-27 15:22:11

JavaScript開發(fā)編程語言

2024-08-21 08:37:47

CodeEmmet懸浮框

2017-01-22 12:41:02

CinnamonLinux桌面

2021-03-11 09:02:37

SQL數(shù)據(jù)庫數(shù)據(jù)

2010-11-10 09:01:50

Visual Stud

2022-01-11 06:53:23

腳本編碼Python

2014-04-29 11:07:45

藍牙云計算

2025-03-25 08:45:00

C#編程漏洞

2023-12-27 14:12:40

JavaScrip技巧

2009-06-30 19:13:53

數(shù)據(jù)中心微軟服務(wù)器

2010-07-05 10:52:42

水冷超級計算機

2019-04-01 06:37:12

R語言數(shù)據(jù)分析數(shù)據(jù)

2021-07-16 11:57:19

公共云云計算云服務(wù)

2021-12-16 23:02:57

前端功能JavaScript

2014-10-23 08:56:42

開源項目C

2009-07-03 17:09:01

學習Tapestry

2025-04-08 00:07:37

語法糖C#代碼

2013-03-29 11:09:44

蘋果里諾數(shù)據(jù)中心
點贊
收藏

51CTO技術(shù)棧公眾號