(*) Delegate
Start with the simplest example:
Namespace ConsoleApplication1
{
Class Program
{
// The delegate is actually equivalent to a type. Here, the type is called BinaryOp.
Public delegate int BinaryOp (int x, int y );
Static public int Add (int x, int y)
{
Return x + y;
}
Static void Main ()
{
// Create a BinaryOp instance and use the static Add method for initialization.
BinaryOp d = new BinaryOp (Add );
Console. WriteLine (d (10, 10 ));
}
}
}
The above is the delegate of the static method. Next we will look at the delegate of an instance method.
Class MyClass
{
Private string name;
Public MyClass (string name)
{
This. name = name;
}
Public void DisplayName ()
{
Console. WriteLine ("{0}", name );
}
}
Class Program
{
// The delegate is actually equivalent to a type. Here, the type name is SimpleDelegate.
Public delegate void SimpleDelegate ();
Static void Main ()
{
MyClass a = new MyClass ("");
MyClass B = new MyClass ("B ");
// Initialize with DisplayName of the instance method
SimpleDelegate d = new SimpleDelegate (a. DisplayName );
D ();
D = new SimpleDelegate (B. DisplayName );
D ();
}
}
(*) Event
A delegate is a type, and an event is a member. See the following code:
Namespace ConsoleApplication1
{
Public class SimpleMath
{
Public delegate int BinaryOp (int a, int B); // defines the Binary type.
Public event BinaryOp BinaryEvent; // defines BinaryEvent members.
Public int Add (int a, int B) {return a + B ;}
Public int Substract (int a, int B) {return a-B ;}
Public int Calculate ()
{
// Raise the event by using the () operator.
Return BinaryEvent (1, 2); // It can only be called within the class that defines the event. If it is written outside, it cannot be compiled.
}
}
Class Program
{
Static void Main ()
{
SimpleMath sm = new SimpleMath ();
// Sm. BinaryEvent (1, 2); Compilation error! It can only be called within the class that defines the event
// The following two registration methods have the same effect, which is equivalent to registering twice and will indeed be executed twice in sequence.
Sm. BinaryEvent + = new SimpleMath. BinaryOp (sm. Add );
Sm. BinaryEvent + = sm. Add;
Console. WriteLine (sm. Calculate (); // The result is 3.
// The following two registration methods have the same effect, which is equivalent to registering twice and will indeed be executed twice in sequence.
Sm. BinaryEvent + = new SimpleMath. BinaryOp (sm. Substract );
Sm. BinaryEvent + = sm. Substract;
Console. WriteLine (sm. Calculate (); //-1, retain only the returned values of the last call (3,-1,-1)
}
}
}
(*) Anonymous Method
The role of the anonymous method is to simplify the code and help programmers. If there is no anonymous method, define an event listener as follows:
Class Program
{
Public delegate void SomeDelegate (); // defines the delegate
Static void Main ()
{
SomeType obj = new SomeType ();
Obj. SomeEvent + = new SomeDelegate (MyEventHandler );
}
// In general, the defined MyEventHandler method is only used to respond to events, and is used only in the above place
Public static void MyEventHandler () // defines the method called by the delegate
{}
}
The code above is too long-winded, especially for the name of the Delegate and the method it calls. With the anonymous method, you only need:
Class Program
{
Static void Main ()
{
SomeType obj = new SomeType ();
Obj. SomeEvent + = delegate {
// Implement event processing logic
}; // Note that a semicolon is required
}
}
Compared with the previous Code, SomeDelegate and MyEventHandler are not supported.