I. The definition of a delegate:
"Advanced Chinese Dictionary" is explained as follows: entrusted to other people or institutions to deal with. To say that the meaning of life in fact everyone can understand, is nothing but "when someone (organization) needs to complete a thing that they can not or should not be done, the person (organization) is looking for a suitable person who is capable of accomplishing this, and then provides the necessary information to entrust the matter to the identified person (institution) for completion. "A delegate in C # is a type of reference method, and once a method is assigned to a delegate, the delegate behaves exactly like the method, and the use of the delegate method can have parameters and return values as any other method." A delegate object can be passed to code that invokes the method reference without knowing which method will be invoked at compile time. A delegate is the encapsulation of a function, which represents a "class" function. They all meet a certain signature: they have the same argument list, return value type. Delegates can also be viewed as abstractions of functions, which are "classes" of functions. At this point, the delegate instance represents a specific function. A delegate should be a level of similarity, and it is very much like a class. Let's take a look at an instance of a delegate using:
public delegate void Printhandler (String str); Declare delegate type public
class Printstr
{public
void Callprint (string input)
{
Console.WriteLine (input);
}
static void Main (string[] args)
{
Printstr myprinter = new Printstr ();
Printhandler MyHandler = null;
The delegate is linked to a method to instantiate the delegate
MyHandler + = new Printhandler (myprinter.callprint);
if (MyHandler!= null)
MyHandler ("Hello world!"); Invokes the delegate, which is equivalent to the method linked by the anonymous invocation delegate
MyHandler-= new Printhandler (myprinter.callprint);
if (MyHandler = = null)
Console.WriteLine ("Myhandler==null");
Console.read ();
}
The results obtained are
Hello world!
Myhandler==null