委派之小筆記(2)
委派小筆記(1)記錄了非常基礎的委派使用方式,這邊再記錄一下導入泛型機制後的委派該怎麼使用。
程式碼如下:
1: namespace DelegateTrain
2: {
3:
4: public delegate string BossDel3<T>(T name);
5:
6: class Boss
7: {
8: public delegate void BossCall();
9: }
10:
11: class Employee
12: {
13:
14: public string doWorkGen(string x)
15: {
16: return string.Format("{0} : 泛型委派",x);
17: }
18: }
19:
20: class Program
21: {
22: static void Main(string[] args)
23: {
24: Employee employee = new Employee();
25:
26: BossDel3<string> bossDel3 = new BossDel3<string>(employee.doWorkGen);
27: Console.WriteLine(bossDel3("大胖"));
28: Console.ReadLine();
29: }
30: }
31: }
上面程式碼(23)展示了第二種將方法名稱指定給委派的方式!(是不是很容易呢^____^)