Delegate keywords and delegate types

Source: Internet
Author: User
I wrote an example today to illustrate the differences between delegate and delegate. The post is as follows, and the comments are complete:

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 * In this position, the compiler generates a consoleapplication. testdel (inherited from system. multicastdelegate) type for us due to the delegate keyword)
12 * The constructor of this type is defined:
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) cel 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) cel 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 }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.