[.NET] C# 委託

來源:互聯網
上載者:User

標籤:source   ssi   art   following   invoke   follow   ima   using   from   

C# 委託

【原文】http://www.cnblogs.com/liqingwen/p/6046171.html                      

  上篇《C# 知識回顧 - 委託 delegate》已經介紹委託的基本知識,這裡是對其進行補充說明及加深理解。

 

目錄

 

一、兩個簡單 Demo:帶命名方法的委託和帶匿名方法的委託

  委託可以與命名方法關聯。  使用命名方法對委託進行執行個體化時,該方法將作為參數傳遞,例如:  

 1     class Program 2     { 3         //聲明一個委託 4         delegate void MyDel(string message); 5  6          7         static void Main(string[] args) 8         { 9             //使用靜態方法作為參數執行個體化委託10             MyDel del = Print;11         }12 13         //聲明一個方法14         private static void Print(string message)15         {16             Console.WriteLine(message);17         }18     }

  這被稱為使用命名的方法。  使用命名方法構造的委託可以封裝靜態方法或執行個體方法。在早期版本的 C# 中,命名方法是對委託進行執行個體化的唯一方式。  但是,在不希望付出建立新方法的系統開銷時,C# 使您可以對委託進行執行個體化,並立即指定委託在被調用時將處理的代碼塊。  代碼塊可以包含 lambda 運算式或匿名方法。  

  【備忘】①作為委託參數傳遞的方法必須與委託聲明具有相同的簽名。②委托執行個體可以封裝靜態或執行個體方法。③儘管委託可以使用 out 參數,但建議您不要將其用於多路廣播事件委託,因為您無法知道哪個委託將被調用。   樣本1:以下是聲明及使用委託的一個簡單樣本。  注意,委託 MyDel 和關聯的方法 Print 具有相同的簽名(即便方法的參數名稱 m 和 n 的位置替換)  。
 1     class Program 2     { 3         //聲明一個委託 4         delegate void MyDel(int n, int m); 5  6         static void Main(string[] args) 7         { 8             //使用靜態方法 Print 作為參數執行個體化委託 9             MyDel del = Print;10             Console.WriteLine("準備好了哦,要開始調用委託了哦!");11 12             for (int i = 0; i < 10; i++)13             {14                 Print(i, 1);15             }16 17             Console.Read();18         }19 20         //聲明一個方法21         private static void Print(int m, int n)22         {23             Console.Write(m - n + " ");24         }25     }

 

   樣本2:通過匿名方法執行個體化委託,修改樣本1,結果同上。

    class Program    {        //聲明一個委託        delegate void MyDel(int n, int m);        static void Main(string[] args)        {            //使用靜態方法 Print 作為參數執行個體化委託            //MyDel del = Print;            //使用匿名方法            MyDel del = delegate (int m, int n)              {                  Console.Write(m - n + " ");              };            Console.WriteLine("準備好了哦,要開始調用委託了哦!");            for (int i = 0; i < 10; i++)            {                Print(i, 1);            }            Console.Read();        }    }

 

 二、建立多播委託

  本樣本示範如何建立多播委託。委派物件的一個有用屬性是:

  可以使用 + 運算子將多個對象分配給一個委託執行個體。多播委託包含已指派委託的列表。在調用多播委託時,它會按順序調用列表中的委託。只能合并相同類型的委託。- 運算子可用於從多播委託中移除組件委託。

 1     class Program 2     { 3         //聲明一個委託 4         delegate void MyDel(); 5  6         static void Main(string[] args) 7         { 8             //Action:你也可以自己嘗試使用 Action 代替 MyDel 試試 9 10             MyDel del = Start;  //建立一個委派物件11             MyDel del2 = Stop;  //建立一個委派物件12             MyDel del3 = del + Stop;    //合并前兩個委派物件13             MyDel del4 = del3 - Start;  //移除一個委派物件14 15             Console.WriteLine($"This is {nameof(del)}: ");16             del();17             Console.WriteLine($"This is {nameof(del2)}: ");18             del2();19             Console.WriteLine($"This is {nameof(del3)}: ");20             del3();21             Console.WriteLine($"This is {nameof(del4)}: ");22             del4();23 24             Console.Read();25         }26 27         private static void Start()28         {29             Console.WriteLine($"    {nameof(Start)}...");30         }31 32         private static void Stop()33         {34             Console.WriteLine($"    {nameof(Stop)}!");35         }36     }

 

--預覽版,整理中--

[.NET] C# 委託

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.