【C#】委託

來源:互聯網
上載者:User

標籤:style   blog   http   color   使用   os   io   資料   

 

一、委託的基本的寫法
  internal class Program    {        private static void Main(string[] args)        {            ChainDelegate();            Console.ReadKey();        }        public static void ChainDelegate()        {            //建立委託            Feedback feedbackToConsole = new Feedback(FeedbackToConsole);            Feedback feedbackToMsBox = new Feedback(FeedbackToMsBox);                        //單個委託            feedbackToConsole(1);            feedbackToMsBox(2);            //委託鏈            Feedback fbChain = null;            fbChain += feedbackToConsole;            fbChain += feedbackToMsBox;            fbChain(3);        }        public static void FeedbackToConsole(Int32 value)        {            Console.WriteLine(string.Concat("FeedbackToConsole->", value));        }        public static void FeedbackToMsBox(Int32 value)        {            Console.WriteLine(string.Concat("FeedbackToMsBox->", value));        }    }    //定義一個委託    internal delegate void Feedback(Int32 value);
View Code

 知識點:

  1.定義delegate:delegate void Feedbakc(Int32 value)

    • delegate是關鍵字
    • void傳回值(這裡可以定義各種傳回值,包括泛型)
    • Int32 value定義了輸入的參數             

  2.定義回呼函數FeedbackToConsole, FeedbackToMsBox

    • 回調的函數的輸入參數類型和個數要和定義的委託完全一樣
    • 傳回值是參考型別要遵守協變和逆變(這裡可以看我轉載的泛型3),實值型別不用遵守遵守協變和逆變。

  3.建立委託 var feedbackToConsole=new Feedback(FeedbackToConsole)

    • 用new關鍵字來建立委託
    • 參數是你的回呼函數(要求看第2點)

  4.調用委託

      feedbackToConsole(1)

      feedbackToConsole.Invoke(1)

    • 當建立好委託以後,所對應對象名(feedbackToConsole)就相當於委託的函數(FeedbackToConsole)。

  5.委託鏈

     Feed fbChain=null;
     fbChain+=feedbackToConsole;      fbChain=(Feedback)Delegate.Combine(fbChain,feedbackToConsole);
              fbChain+=feedbackToMsBox;        fbChain=(Feedback)Delegate.Combine(fbChain,feedbackToMsBox);   

    • 有兩種寫法都列出來了,我喜歡第一種,比較簡單和直接。
    • 委託鏈的調用和調用單個一樣 fbChain(3)
    • 委託鏈的調用循序和綁定上去的循序一致(這裡先調用feedbackToConsole,再調用feedbackToMsBox) 

    6.移除委託鏈中的資料(補充)
       fbChain -= feedbackToConsole         fbChain=(Feedback)Delegate.Remove(fbChain,feedbackToConsole);  

  • 二、委託和Lamda運算式   
         Feedback fd1 = new Feedback(i =>            {                Console.WriteLine(i);            });

    我們這樣用Lamda運算式直接代替了回呼函數,如果回呼函數不是多個地方使用的話,我個人是比較喜歡這樣的寫法,而且我會寫成:

     Feedback fd1 = new Feedback(Console.WriteLine);

    這塊需要大家對Lamda運算式有一定的理解。

  • 三、泛型委派
  •  1.泛型委派和泛型介面一樣,是有協變和逆變的(這裡可以看我轉載的泛型3)
     2.泛型委派我們自己一般是不需要去建立的,因為C#自己提供了兩種:
          Action<T,......> :只帶有輸入參數的泛型委派(有N個重載)
              Func<in TInput,......out TOutput> : 帶輸入參數和傳回值的泛型委派(有N個重載)

     3.關於EvenHandler<T>可以看一下【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.