delegate關鍵字與Delegate類型

來源:互聯網
上載者:User
今天寫了一個例子,能說明delegate和Delegate的不同,帖在下面,注釋很全:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Text;
 4 using System.Resources;
 5 using System.Reflection;
 6 using System.Globalization;
 7 
 8 namespace ConsoleApplication
 9 {
10   /*
11    * 在這個位置,由於delegate關鍵字,編譯器會為我們產生一個類型 ConsoleApplication.TestDel (繼承自System.MulticastDelegate)
12    * 而這個類型的構造器定義為:
13    * .method public hidebysig specialname rtspecialname 
14         instance void  .ctor(object 'object',
15                              native int 'method') runtime managed
16       {
17       } // end of method TestDel::.ctor
18    */
19   delegate int TestDel(int i);
20 
21   class UseDelegate
22   {
23     /*
24      * .method public hidebysig static string  Compute(class ConsoleApplication.TestDel del,
25                                                 int32 input) cil managed
26      */
27     public static string Compute(TestDel del, int input)
28     {
29       return del(input).ToString();
30     }
31 
32     /*
33      * .method public hidebysig static string  ComputeTwo(class [mscorlib]System.Delegate del,
34                                                    int32 input) cil managed
35      */
36     public static string ComputeTwo(Delegate del, int input)
37     {
38       return null;
39     }
40   }
41 
42 
43   class Program
44   {
45     static void Main(string[] args)
46     {
47       int k = 5;
48 
49       /*
50        *  IL_0004:  ldftn      int32 ConsoleApplication.Program::MyMethodOne(int32)
51           IL_000a:  newobj     instance void ConsoleApplication.TestDel::.ctor(object,
52                                                                                native int)
53           IL_000f:  ldloc.0
54           IL_0010:  call       string ConsoleApplication.UseDelegate::Compute(class ConsoleApplication.TestDel,
55                                                                               int32)
56           IL_0015:  call       void [mscorlib]System.Console::WriteLine(string)
57        */
58       Console.WriteLine(UseDelegate.Compute(MyMethodOne, k));
59       Console.WriteLine(UseDelegate.Compute(MyMethodTwo, k));
60 
61       /*
62        * IL_0034:  ldftn      int32 ConsoleApplication.Program::MyMethodOne(int32)
63         IL_003a:  newobj     instance void ConsoleApplication.TestDel::.ctor(object,
64                                                                              native int)
65         IL_003f:  ldloc.0
66         IL_0040:  call       string ConsoleApplication.UseDelegate::ComputeTwo(class [mscorlib]System.Delegate,
67                                                                                int32)
68         IL_0045:  call       void [mscorlib]System.Console::WriteLine(string)
69        */
70       Console.WriteLine(UseDelegate.ComputeTwo(new TestDel(MyMethodOne), k));
71 
72       Console.Read();
73     }
74 
75     private static int MyMethodOne(int i)
76     {
77       return (i++);
78     }
79 
80     private static int MyMethodTwo(int i)
81     {
82       return (i--);
83     }
84 
85   }
86 
87 }

聯繫我們

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