Public delegate void my delegate (stringstr);
A delegate is a special class that encapsulates a method. This method is automatically called when the event occurs. The benefits are obvious. It enables users to customize their own method implementations. Through encapsulation, when the corresponding event is triggered, the CLR will call the method you defined to realize your function.
be filled/suffused/brimming with
The definition of 1. delegate is similar to the definition of method, except that it is preceded by a delegate, but the delegate is not a method, but a type. Is a special type, which is easier to understand as a new object type. Used to check the relationship with commission.
Method calls with the same signature.
2. A delegate is equivalent to a function pointer in C++, but it is type-safe.
3. The delegation is derived from the system. Delegate, but it cannot be directly derived from the system. Delegates are just like defining regular types. Delegate declarations can only be defined by the above declaration formats. Keyword delegate informs the compiler that this is a
A delegate type to encapsulate classes at compile time, and C# defines a special syntax for this process to handle this process.
4. You can't derive from the delegate type, because it is also sealed by default.
5. Delegates can call static methods and instance methods.
6. Each delegate type contains its own call list. When a delegate is merged or deleted from a delegate, a new call list will be generated.
7. Two different types of delegates are still two different types of delegates, even if they have the same signature and return value. But it is actually the same in use.
Entrustment comparison
Two operators = = and! =
Two delegates are equal in the following cases:
1. When both delegates are empty.
2. When both delegates are not empty, the following conditions are equal.
A. When the call list of two representatives contains only one entry point.
Are equal in the following situations.
(1) calls the same static method of the same object.
(2) Call the same instance method of the same object
B when two delegations have multiple entry points.
Are equal in the following situations.
(1) Only when the called methods in the call list correspond to the same object and the same method of the object one by one.
When the two different types of delegates mentioned above have the same signature and return value, as long as the above conditions are met, the comparison results are the same even if they are different types.
Exception handling of delegation
When an exception occurs in the method calling the delegate, first search for the catch statement block in the method calling the delegate. If not, go to the method called by the delegate to see if there is a catch statement block, which is different from the place where the calling method is abnormal.
The reason is the same.
NullRefrenceException occurs when an empty delegate is called, that is, when the calling method is not in the list of delegates.
Notes for delegations:
When a delegate has multiple entry points, the calling delegate will be called in the order of the methods in the call list of the delegate. These methods * * * share a parameter set, so when the delegate has a return value, the return value after calling the delegate is the most.
The return value of the latter method either has an out parameter. If the parameter of the delegate is ref (reference type), if the value of this parameter is changed when the first method is hosted, this change will affect subsequent method calls.
An example of authorization
Use the system;
Use the system. Assemble. Generics;
Use the system. Text;
Namespace ConsoleApplication 1
{
Class plan
{
Static void Main(string[] args)
{
//Create a delegate instance to encapsulate the static method M 1 of class C..
MyDelegate d 1 = new mydelegate (c.m1);
d 1(" d 1 "); // M 1
//Create a delegate instance to encapsulate the static method M2 of class C.
My representative d2 = new my representative (m2);
D2(“D2”); // M2
//Create a delegate instance to encapsulate the instance method M3 of class C.
my delegate D3 = new my delegate(new C(). m3);
D3(“D3”); // M3
//Create a delegate instance from delegate d3.
MyDelegate d4 = new mydelegate (D3);
D4(“D4”); // M3
//Combine two delegates
my delegate D5 = d 1+D2;
D5+= D3;
D5(“D5”); // M 1,M3 M2
//Delete d3 from the combined delegation.
my delegate D6 = D5-D3;
D6(《D6》); // M 1,M2
D6-= D3; //Although d3 is no longer in the d6 call list, it is impossible to delete it and there will be no error.
D6(《D6》); // M 1,M2
D6-= D6;
//D6(《D6》); At this point, d6' s call list is empty, d6 is empty, so the system. Throws NullReferenceException.
MyDelegate d7 = new MyDelegate(C 1. p 1);
D7(“D7”); // C 1。 P 1
MyDelegate d8 = new MyDelegate (new C2 (). p1);
D8(“D8”); // C2。 P 1
}
}
//declare a delegate MyDelegate.
Public delegate void my delegate (stringstr);
Public class c
{
The public static void M 1(string str)// static method m 1 in class C (static added).
{
Console. WriteLine("From:C.M 1:",str);
}
Public static empty M2 (string)
{
Console. WriteLine("From:C.M2:",str);
}
Public empty M3 (string)
{
Console. WriteLine("From:C.M3:",str);
}
}
Public class C 1
{
Public static void P 1 (string str)
{
Console. WriteLine("From:C 1。 P 1:",str);
}
}
Common class C2
{
Public void P 1 (string str)
{
Console. WriteLine ("From: C2. P 1:",str);
}
}
}
Event delegation
Overview of activities
An event is a message or notification sent by an object or class when its state changes. The object or class that sends out the information is called the "event source" and the method of handling the event is called the "receiver". Usually, when an event source sends out state change information, it doesn't know which event receiver will handle it. This requires a management mechanism to coordinate the event source and receiver, which is completed by function pointers in C++. In C#, events use delegates to provide type-safe encapsulation for methods that will be called when triggered.
Event statement
1. Declare a delegate.
Public delegate void EventHandler (object sender, system. EventArgs e);
Announce an event
The public event EventHandler has changed;
trigger event
Public Online Change (Environmental Label)
{
If (changed! = empty)
{
Changed (this, e);
}
}
4. Define event handlers
Public MyText_OnChanged (object sender, EventArgs e)
5. Subscribe to the event (add the event handler to the call list of the event)
My text message. changed+= EventHandler(my text _ on changed);
The following small example illustrates how to define a complete event mechanism:
Use the system;
Use the system. Assemble. Generics;
Use the system. Text;
Namespace ConsoleApplication 1
{
Class plan
{
Static void Main(string[] args)
{
my text my text = new my text();
//Add the event handler to the call list of the event (that is, event routing).
My text message. Changed += new MyText. ChangedEventHandler(my text _ Changed);
string str =
while (str! = "Exit")
{
Console. WriteLine ("Please enter a string:");
Str = console. ReadLine();
My text message. Text = str
}
}
//Process of handling change events
Private static void myText_Changed (object sender, EventArgs e)
{
Console. WriteLine ("Text changed: n" ,((MyText)sender). Text);
}
}
Public class MyText
{
private string _ text =
//Define the delegate of the event.
Public delegate void ChangedEventHandler (object sender, eventargese);
//Define an event
The public event ChangedEventHandler has been changed;
//Used to trigger a change event.
Protected virtual void OnChanged(EventArgs e)
{
If (this. It has changed! = empty)
This. Changed (this, e);
}
//Text attribute
Common string text
{
Get {Returns this. _ text}
set up
{
This. _ text = value
//Trigger the Change event when the text changes.
This. on changed(new EventArgs());
}
}
}
}