<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      委托

      一.委托

         委托可以理解為一種數據類型(delegate),后面根函數定義.

      隱藏行號 復制代碼 這是一段程序代碼。
      1. delegate void Del(int x);
        

       

      委托概述

      委托具有以下特點:

      • 委托類似于 C++ 函數指針,但它是類型安全的。

      • 委托允許將方法作為參數進行傳遞

      • 委托可用于定義回調方法。

      • 委托可以鏈接在一起;例如,可以對一個事件調用多個方法。

      • 方法不需要與委托簽名精確匹配。有關更多信息,請參見協變和逆變

      • C# 2.0 版引入了匿名方法的概念,此類方法允許將代碼塊作為參數傳遞,以代替單獨定義的方法。

      二.委托使用.

         1.定義一個簽名

         2.定義委托變量,并為委托變量賦值(可以是靜態方法/實例.方法).

         3.調用.

      隱藏行號 復制代碼 這是一段程序代碼。
      1. delegate void Del(int i, double j);  //1,定義一個簽名
        
      2. 
        
      3. class MathClass
        
      4. {
        
      5.     static void Main()
        
      6.     {
        
      7.         MathClass m = new MathClass();
        
      8. 
        
      9.         // Delegate instantiation using "MultiplyNumbers"
        
      10.         Del d = m.MultiplyNumbers;   //2定義一個委托變量,為委托變量賦值
        
      11. 
        
      12.         // Invoke the delegate object.
        
      13.         System.Console.WriteLine("Invoking the delegate using 'MultiplyNumbers':");
        
      14.         for (int i = 1; i <= 5; i++)
        
      15.         {
        
      16.             d(i, 2);   //3.調用
        
      17.         }
        
      18.     }
        
      19. 
        
      20.     // Declare the associated method.
        
      21.     void MultiplyNumbers(int m, double n)
        
      22.     {
        
      23.         System.Console.Write(m * n + " ");
        
      24.     }
        
      25. }
        

      三.匿名方法  委托.

         要將代碼塊傳遞為委托參數,創建匿名方法則是唯一的方法

         將代碼塊賦值給一個匿名變量.

      隱藏行號 復制代碼 這是一段程序代碼。
      1. // Create a delegate instance
        
      2. delegate void Del(int x);
        
      3. 
        
      4. // 將代碼塊賦值給一個委托
        
      5. 
        
      6. Del d = delegate(int k) { /*語句1...語句2 do some ... */ };
        

      四.何時使用委托而不使用接口(C# 編程指南)

      委托和接口都允許分離類型聲明和實現。給定的接口可由任何實現;可以為任何類中的方法創建委托,前提是該方法符合委托的方法簽名。接口引用或委托都可由不了解實現該接口或委托方法的類的對象使用。既然存在這些相似性,那么何時應使用委托,何時又該使用接口呢?

      在以下情況中使用委托:

      • 當使用事件設計模式時。

      • 當封裝靜態方法可取時。

      • 當調用方不需要訪問實現該方法的對象中的其他屬性、方法或接口時。

      • 需要方便的組合。

      • 當類可能需要該方法的多個實現時。

      在以下情況中使用接口:

      • 當存在一組可能被調用的相關方法時。

      • 當類只需要方法的單個實現時。

      • 當使用接口的類想要將該接口強制轉換為其他接口或類類型時。

      • 當正在實現的方法鏈接到類的類型或標識時:例如比較方法:使用單一方法接口而不使用委托的一個很好的示例是 IComparableIComparableIComparable 聲明 CompareTo 方法,該方法返回一個整數,以指定相同類型的兩個對象之間的小于、等于或大于關系。IComparable 可用作排序算法的基礎,雖然將委托比較方法用作排序算法的基礎是有效的,但是并不理想。因為進行比較的能力屬于類,而比較算法不會在運行時改變,所以單一方法接口是理想的。

       

      五.協變.

      隱藏行號 復制代碼 這是一段程序代碼。
      1. class Mammals
        
      2. {
        
      3. }
        
      4. 
        
      5. class Dogs : Mammals
        
      6. {
        
      7. }
        
      8. 
        
      9. class Program
        
      10. {
        
      11.     // Define the delegate.
        
      12.     public delegate Mammals HandlerMethod();
        
      13. 
        
      14.     public static Mammals FirstHandler()
        
      15.     {
        
      16.         return null;
        
      17.     }
        
      18. 
        
      19.     public static Dogs SecondHandler()
        
      20.     {
        
      21.         return null;
        
      22.     }
        
      23. 
        
      24.     static void Main()
        
      25.     {
        
      26.         HandlerMethod handler1 = FirstHandler;
        
      27. 
        
      28.         // Covariance allows this delegate.
        
      29.         HandlerMethod handler2 = SecondHandler;
        
      30.     }
        
      31. }
        

      六.逆變

      隱藏行號 復制代碼 這是一段程序代碼。
      1. System.DateTime lastActivity;
        
      2. public Form1()
        
      3. {
        
      4.     InitializeComponent();
        
      5. 
        
      6.     lastActivity = new System.DateTime();
        
      7.     this.textBox1.KeyDown += this.MultiHandler; //works with KeyEventArgs
        
      8.     this.button1.MouseClick += this.MultiHandler; //works with MouseEventArgs
        
      9. 
        
      10. }
        
      11. 
        
      12. // Event hander for any event with an EventArgs or
        
      13. // derived class in the second parameter
        
      14. private void MultiHandler(object sender, System.EventArgs e)
        
      15. {
        
      16.     lastActivity = System.DateTime.Now;
        
      17. }
        

      七.合并

      隱藏行號 復制代碼 這是一段程序代碼。
      1. delegate void Del(string s);
        
      2. 
        
      3. class TestClass
        
      4. {
        
      5.     static void Hello(string s)
        
      6.     {
        
      7.         System.Console.WriteLine("  Hello, {0}!", s);
        
      8.     }
        
      9. 
        
      10.     static void Goodbye(string s)
        
      11.     {
        
      12.         System.Console.WriteLine("  Goodbye, {0}!", s);
        
      13.     }
        
      14. 
        
      15.     static void Main()
        
      16.     {
        
      17.         Del a, b, c, d;
        
      18. 
        
      19.         // Create the delegate object a that references 
        
      20.         // the method Hello:
        
      21.         a = Hello;
        
      22. 
        
      23.         // Create the delegate object b that references 
        
      24.         // the method Goodbye:
        
      25.         b = Goodbye;
        
      26. 
        
      27.         // The two delegates, a and b, are composed to form c: 
        
      28.         c = a + b;
        
      29. 
        
      30.         // Remove a from the composed delegate, leaving d, 
        
      31.         // which calls only the method Goodbye:
        
      32.         d = c - a;
        
      33. 
        
      34.         System.Console.WriteLine("Invoking delegate a:");
        
      35.         a("A");
        
      36.         System.Console.WriteLine("Invoking delegate b:");
        
      37.         b("B");
        
      38.         System.Console.WriteLine("Invoking delegate c:");
        
      39.         c("C");
        
      40.         System.Console.WriteLine("Invoking delegate d:");
        
      41.         d("D");
        
      42.     }
        
      43. }
        

       

      輸出
      Invoking delegate a:
        Hello, A!
      Invoking delegate b:
        Goodbye, B!
      Invoking delegate c:
        Hello, C!
        Goodbye, C!
      Invoking delegate d:
        Goodbye, D!
      posted @ 2010-05-10 20:54  SouthAurora  Views(211)  Comments(0)    收藏  舉報
      主站蜘蛛池模板: 97人人添人澡人人爽超碰| 日韩AV高清在线看片| 国产在线永久视频| 中文字幕va一区二区三区| 国产精品视频一区二区不卡| 中文字幕在线精品视频入口一区| 国内精品免费久久久久电影院97 | 四虎精品视频永久免费| 亚洲精品一区二区三区婷婷月| 亚洲鸥美日韩精品久久| 国产精品免费看久久久无码 | 精品无码国产污污污免费| 成人免费AA片在线观看| 远安县| 久久97超碰色中文字幕| 中文字幕国产精品二区| 日本一二三区视频在线| 日本熟妇人妻一区二区三区| 中文字幕亚洲制服在线看| 佛冈县| 日日爽日日操| 116美女极品a级毛片| 亚洲人妻一区二区精品| 亚洲春色在线视频| 久久精品国产亚洲AV成人毛片| 强奷乱码欧妇女中文字幕熟女| 无码中文av波多野结衣一区| 最新永久免费AV无码网站| 国产中文字幕日韩精品| 日韩精品一二区在线观看| 久久国产乱子伦免费精品无码 | 国产精品三级国产精品高| 四虎影院176| 黑人玩弄人妻中文在线| 精品无码国产污污污免费| 奇台县| 无码h片在线观看网站| 一区二区中文字幕视频| 国产欧美日韩免费看AⅤ视频| 九九热在线视频观看精品| 欧美xxxxx高潮喷水|