I. C # Delegate class
A delegate is similar to a function pointer, but a function pointer can only reference static methods. A delegate can reference both static methods and instance methods.
Delegated use is divided into three steps: 1. Delegate Declaration; 2. Delegate instantiation; 3. Delegate call.
Example 1:
Program code
1. Using system;
2.
3. namespace Delegation
4 .{
5. Delegate int numope (int A, int B); // Step 1: Delegate statement
6. Class class1
7 .{
8. Static void main (string [] ARGs)
9 .{
10. class1 C1 = new class1 ();
11. numope p1 = new numope (c1.add); // delegate instantiation. Note that the parameter is the name of the parameter to be used without parentheses.
12. Console. writeline (p1 (1, 2); // delegate call
13. Console. Readline ();
14 .}
15.
16. Private int add (INT num1, int num2)
17 .{
18. Return (num1 + num2 );
19 .}
20 .}
21 .}
22.
In this example, the numope delegate references the add method.
After the delegate is declared, it can be instantiated like a class. During the instantiation, the referenced method (such as: add) is used as a parameter, so that the delegate and method are associated, you can use the delegate to reference the method.
The delegate and the referenced method must be consistent:
1. The number, type, and order of parameters must be completely consistent.
2. The return values must be consistent.
II. C # events
There are many events, such as mouse events: mousermove, mouserdown, and Keyboard Events: keyup, keydown, and keypress.
If there is an event, there will be a way to process the event. How is the relationship between the event and the handling method? A delegate is a bridge between them. When an event occurs, the Delegate will know and pass the event to the handling method for corresponding processing.
For example, the most common button click event in winform is the delegate: This. button1.click + = new system. eventhandler (this. button#click); after the button is pressed, The button#click method is started for processing. Eventhandler is a token that has been declared in the system class library. ------- Delegate instantiation, this. buttionmediaclick is the method name
III. C # custom events and their handling
Eventhandler and other Custom Event delegation are special types of delegation, which have the same form:
Delegate void event Delegate name (Object sender, eventargs E );
The object is used to pass the event occurrence. For example, the button control in section 2 is an event occurrence, and eventargs is used to pass the event details.
Example 2:
Program code
23. Using system;
24.
25. The simplest Custom Event in namespace
26 .{
27. // <summary>
28. // event sending class
29. // </Summary>
30. Class class1
31 .{
32. Public Delegate void userrequest (Object sender, eventargs E); // defines the delegate
33. Public event userrequest onuserrequest; // defines a delegate-type event
34.
35. Public void run ()
36 .{
37. While (true)
38 .{
39. If (console. Readline () = "")
40. {// event listening
41. onuserrequest (this, new eventargs (); // generates an event
42 .}
43 .}
44 .}
45 .}
46.
47. // <summary>
48. // event receiving Class
49. // </Summary>
50. Class class2
51 .{
52. Static void main (string [] ARGs)
53 .{
54. class1 C1 = new class1 ();
55. c1.onuserrequest + = new class1.userrequest (c1_onuserrequest); // bind to the event after the delegate is instantiated
56. c1.run ();
57 .}
58.
59. Private Static void c1_onuserrequest (Object sender, eventargs E)
60. {// event handling method
61. Console. writeline ("/t you triggered the event! ");
62 .}
63 .}
64 .}
65.
Example 3:
Program code
66. Using system;
67.
68. events with event data in namespace
69 .{
70. // <summary>
71. // event class with event data, inherited from eventargs
72. // </Summary>
73. Class onuserrequesteventargs: eventargs
74 .{
75. Private string inputtext;
76. Public String inputtext
77 .{
78. Get
79 .{
80. Return inputtext;
81 .}
82. Set
83 .{
84. inputtext = value;
85 .}
86 .}
87 .}
88.
89. // <summary>
90. // event sending class
91. // </Summary>
92. Class class1
93 .{
94. Public Delegate void userrequest (Object sender, onuserrequesteventargs E); -----------> declare delegate
95. Public event userrequest onuserrequest; -------> defines an event of the delegate type.
96.
97. Public void run ()
98 .{
99. While (true)
100 .{
101. Console. writeline ("Enter the content :");
102. String A = console. Readline ();
103. // if (a = "")
104 .//{
105. onuserrequesteventargs e1 = new onuserrequesteventargs ();
106. e1.inputtext =;
107. onuserrequest (this, E1 );
108 .//}
109 .}
110 .}
111 .}
112.
113. // <summary>
114. // event receiving Class
115. /// </Summary>
116. Class class2
117 .{
118. [stathread]
119. Static void main (string [] ARGs)
120 .{
121. class1 C1 = new class1 ();
122. c1.onuserrequest + = new class1.userrequest (c1_onuserrequest );
123. c1.run ();
124 .}
125.
126. Private Static void c1_onuserrequest (Object sender, onuserrequesteventargs E)
127 .{
128. Console. writeline ("/t you entered:" + E. inputtext );
129 .}
130 .}
131 .}
132.
The only difference between Example 3 and Example 2 is that a class onuserrequesteventargs is defined and inherited from eventargs.
So far, C # Delegate class, C # event, and C # Custom Event are introduced here.