標籤: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)——委託