C#學習筆記(7)——委託

來源:互聯網
上載者:User

標籤:delegate   文法   ati   reading   ext   namespace   span   main函數   string   

說明(2017-5-29 22:22:50):

1. 文法:public delegate void mydel();這一句在類外面,命名空間裡面。

2. 專門建立一個方法,參數是委託:

public static void test(mydel mdl)
{
  mdl();
}

3. 在main函數裡,調用這個方法,參數是要使用的方法:

test(show);

4. 感覺test這個方法只是一個中轉站,裡面存放委託和參數。

例1:

 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6  7 namespace _04委託 8 { 9     public delegate void mydel();10     class Program11     {12        13         static void Main(string[] args)14         {15             test(show);16             Console.ReadKey();17         }18         public static void test(mydel mdl)19         {20             mdl();21         }22         public static void show()23         {24             Console.WriteLine("吃飯了!");25         }26     }27 }

 例2:

 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6  7 namespace _04委託 8 { 9     public delegate int mydel(int x, int y);10     class Program11     {12 13         static void Main(string[] args)14         {15             test(add, 1, 2);16             Console.ReadKey();17         }18         public static void test(mydel mdl, int x, int y)19         {20             Console.WriteLine(mdl(x, y));21         }22         public static int add(int x, int y)23         {24             return x + y;25         }26     }27 }

 

C#學習筆記(7)——委託

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.