From: http://hi.baidu.com/super311/blog/item/15ff2513ed83f4d3a6866945.html
Delegate statement
Public delegate void MyDelegate (string str );
Note
1. The definition of delegation is similar to the definition of method, but a delegate is added before, but the delegate is not a method, and it is a type. It is a special type and is better understood as a new object type. Used to associate with the delegate
Method call with the same signature.
2. The delegate is equivalent to the function pointer in C ++, but it is type-safe.
3. The Delegate is derived from System. Delegate, but it cannot be directly derived from System. Delegate just like a general type. The Delegate Declaration can only be defined through the preceding Declaration format. The keyword delegate notifies the compiler that this is
This class is encapsulated during compilation. C # defines a special syntax for this process.
4. It cannot be derived from a delegate type because it is also the default sealed
5. Delegate: You can call static methods or instance methods.
6. Each delegate type contains its own call list. A new call list is generated when a delegate is combined or a delegate is deleted from it.
7. Two different types of delegation, even if they have the same signature and return value, are still two different types of delegation. But it can be seen as the same in use.
Delegate comparison
In C #, two operators = and! are defined for the delegate! =
In the following cases, the two delegates are equal:
1. When both delegates are null at the same time
2. When neither delegate is null, the values are equal in the following cases.
A. When the call lists of two delegates contain only one entry point
Are equal in the following cases
(1) call the same static method of the same object
(2) call the same instance method of the same object
B. When two delegates have multiple entry points
Are equal in the following cases
(1) only when the called methods in the call list correspond to the same object and the same method of the object in sequence
When two different types of delegation have the same signature and return value, the comparison results are the same even if they meet the preceding conditions.
Delegate Exception Handling
When an exception occurs in the method that calls the delegate, first search for the catch statement block in the method that calls the delegate. If there is no catch block, go to the method called by the delegate to find out if there is any catch block, which is abnormal with the method called.
The logic is the same.
When a null delegate is called, that is, the list of delegates does not contain a call method, nullrefrenceexception will occur.
Notes for delegation:
When a delegate has multiple entry points, the call delegate will be called in sequence according to the methods in the delegate's call list. these methods share a set of parameters, so when the delegate has a return value, the return value after the delegate is called is the most
The Return Value of the next method or the out parameter exists. if the parameter of the delegate is REF (reference type), if the value of this parameter is changed during the first method reception, this change will affect subsequent method calls.
Example:
Using system;
Using system. Collections. Generic;
Using system. text;
Namespace consoleapplication1
{
Class Program
{
Static void main (string [] ARGs)
{
// Create a delegate instance and encapsulate the static method M1 of class C
Mydelegate d1 = new mydelegate (C. M1 );
D1 ("d1"); // M1
// Create a delegate instance and encapsulate the static m2 method of class C
Mydelegate D2 = new mydelegate (C. m2 );
D2 ("D2"); // m2
// Create a delegated instance and encapsulate Class C instance method m3
Mydelegate D3 = new mydelegate (New C (). m3 );
D3 ("D3"); // m3
// Create a delegate instance from a delegate D3
Mydelegate D4 = new mydelegate (D3 );
D4 ("D4"); // m3
// Combine two delegates
Mydelegate D5 = D1 + D2;
D5 + = d3;
D5 ("D5"); // M1, M2, M3
// Delete d3 from the composite delegate
MyDelegate d6 = d5-d3;
D6 ("D6"); // M1, M2
D6-= d3; // although there is no d3 in the d6 call list, this is only impossible to remove and no error occurs.
D6 ("D6"); // M1, M2
D6-= d6;
// D6 ("D6"); in this case, the D6 call list is empty and D6 is null, causing system. nullreferenceexception
Mydelegate D7 = new mydelegate (c1.p1 );
D7 ("D7"); // c1.p1
Mydelegate D8 = new mydelegate (New C2 (). P1 );
D8 ("D8"); // c2.p1
}
}
// Declare a delegate mydelegate
Public Delegate void mydelegate (string Str );
Public Class C
{
Public static void M1 (string str)
{
Console. WriteLine ("From: C. M1: {0}", str );
}
Public static void M2 (string str)
{
Console. WriteLine ("From: C. M2: {0}", str );
}
Public void M3 (string str)
{
Console. writeline ("from: C. m3: {0}", STR );
}
}
Public Class C1
{
Public static void p1 (string Str)
{
Console. writeline ("from: c1.p1: {0}", STR );
}
}
Public Class C2
{
Public void p1 (string Str)
{
Console. writeline ("from: c2.p1: {0}", STR );
}
}
}
Event
Event Delegate
Event Overview
An event is the information or notification sent by an object or class when the object or class status changes. The object or class that sends the information is called the "event source". The method for processing the event is called the "receiver". Generally, when the event SOURCE sends the status change information
I don't know which event receiver will handle it. in this case, a management mechanism is required to coordinate the event source and receiver, and the function pointer is used in C ++. in C #, events use delegates to provide type-safe encapsulation of called methods for triggering.
Event Declaration
1. Declare a delegate
Public Delegate void eventhandler (Object sender, system. eventargs E );
2. Declare an event
Public event eventhandler changed;
3. Trigger an event
Public onchanged (enventargs E)
{
If (changed! = NULL)
{
Changed (this, e );
}
}
4. Define the event handler
Public mytext_onchanged (Object sender, eventargs E)
{
...
}
5. subscribe to events (add event handlers to the event call list)
Mytext. Changed + = eventhandler (mytext_onchanged );
Example:
Using system;
Using system. Collections. Generic;
Using system. text;
Namespace consoleapplication1
{
Class Program
{Static void main (string [] ARGs)
{
Mytext = new mytext ();
// Add the event handler to the event call list (that is, event wiring)
Mytext. Changed + = new mytext. changedeventhandler (mytext_changed );
String STR = "";
While (STR! = "Quit ")
{
Console. WriteLine ("please enter a string :");
Str = Console. ReadLine ();
MyText. Text = str;
}
}
// Process the Change event
Private static void myText_Changed (object sender, EventArgs e)
{
Console. writeline ("text has been changed: {0} \ n", (mytext) sender). Text );
}
}
Public class mytext
{
Private string _ text = "";
// Define the event Delegate
Public Delegate void changedeventhandler (Object sender, eventargs E );
// Define an event
Public event changedeventhandler changed;
// Used to trigger the change event
Protected virtual void onchanged (eventargs E)
{
If (this. Changed! = NULL)
This. Changed (this, e );
}
// Text attributes
Public String text
{
Get {return this. _ text ;}
Set
{
This. _ text = value;
// Trigger the change event when the text changes
This. onchanged (New eventargs ());
}
}
}
}