溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

什么是.NET Framework委托的預定義方法

發(fā)布時間:2021-06-17 14:30:39 來源:億速云 閱讀:154 作者:chen 欄目:編程語言

這篇文章主要介紹“什么是.NET Framework委托的預定義方法”,在日常操作中,相信很多人在什么是.NET Framework委托的預定義方法問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”什么是.NET Framework委托的預定義方法”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!

.NET Framework開發(fā)環(huán)境對于開發(fā)人員來說是一個非常實用的開發(fā)平臺。我們可以通過它來進行WEB應用程序的部署。前言:從.NET Framework2.0開始以來,系統(tǒng)預定義的委托使使代碼看起來有點像“天書”,再加上匿名表達式,以及后面Lambda表達式的“摻和”,代碼就更加難懂了,于是自己就一點點查MSDN,到現(xiàn)在終于有點入門了。

用.NET Framework委托:

  1. 1、public delegate void Action< T>(T obj )   

  2. 2、public delegate TResult Func< TResult>()  

  3. 3、public delegate bool Predicate< T>(T obj)  

其中:Action和Func還包括從不帶任何參數(shù)到最多四個參數(shù)的重載?

1、public delegate void Action< T>(T obj )

封裝一個方法,該方法只采用一個參數(shù)并且不返回值。

Code

  1. Action< string> messageTarget;   

  2. messageTarget = s => Console.
    WriteLine(s);  

  3. messageTarget("Hello, World!"); 

2、public delegate TResult Func<TResult>()

封裝一個具有一個參數(shù)并返回 TResult 參數(shù)指定的類型值的方法。

類型參數(shù)T

此委托封裝的方法的參數(shù)類型。

TResult

此.NET Framework委托封裝的方法的返回值類型

Code

public static void Main()  {  // Instantiate delegate to reference UppercaseString method  ConvertMethod convertMeth = UppercaseString;  string name = "Dakota";  // Use delegate instance to call UppercaseString method  Console.WriteLine(convertMeth(name));  }  private static string UppercaseString(string inputString)  {  return inputString.ToUpper();  }

3、public delegate bool Predicate<T>(T obj)

表示定義一組條件并確定指定對象是否符合這些條件的方法。

類型參數(shù)

T:要比較的對象的類型。

返回值

如果 obj 符合由此委托表示的方法中定義的條件,則為 true;否則為 false。

Code

  1. public static void Main()  

  2. {  

  3. // Create an array of five Point 
    structures.  

  4. Point[] points = { new Point(100, 200),   

  5. new Point(150, 250), new Point(250, 375),   

  6. new Point(275, 395), new Point(295, 450) };  

  7. Point first = Array.Find(points, ProductGT10);  

  8. // Display the first structure found.  

  9. Console.WriteLine("Found: X = {0}, 
    Y = {1}", first.X, first.Y);  

  10. }  

  11. // This method implements the test 
    condition for the Find  

  12. // method.  

  13. private static bool ProductGT10(Point p)  

  14. {  

  15. if (p.X * p.Y > 100000)  

  16. {  

  17. return true;  

  18. }  

  19. else  

  20. {  

  21. return false;  

  22. }  

到此,關于“什么是.NET Framework委托的預定義方法”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續(xù)學習更多相關知識,請繼續(xù)關注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI