. NET is functionally speaking C or a method pointer in C + +, you can call a delegate to complete a function, like calling a method, or return a class of results. But. NET is a more advanced language, the delegate delegate is also more advanced, the delegate is a data interface, it contains the call target and call the method pointer; NET is inherited from MulticastDelegate, a multicast delegate, which is a delegate that can contain multiple calling methods.
one. Let's look at the definition of the delegate:
The following C # code defines the delegate
public delegate void dosomething (int times);
the definition of a delegate contains 5 parts
1 Public indicates the accessibility of the delegate
2) delegate keyword indicates that a delegate is to be defined
3 void represents the return value of the delegate definition method
4) DoSomething is the name of the commissioned
5) (int times) is the parameter list of the delegate method, where the argument list can include ref parameters or out parameters, as well as parms variable quantity parameters; Note that if there are multiple calling methods in the delegate, Only the calculated value of a delegate method that can return a delegate's last success when using out parameters
Defining a delegate in C # is very simple, only a delegate keyword before the return value of the method definition.
But we know that all user-defined delegates inherit from MulticastDelegate; MulticastDelegate is a class; So the custom delegate is certainly a class; the IL code for the above code can prove our inference:
Copy Code code as follows:
. class public auto ANSI sealed delegates. DoSomething
extends [mscorlib]system.multicastdelegate
{
Methods
. method public Hidebysig specialname rtspecialname
instance void. ctor (
Object ' object ',
Native int ' method '
) runtime managed
{
}//End of method Dosomething::.ctor
. method public hidebysig newslot virtual
instance void Invoke (
Int32 times
) runtime managed
{
}//End of method Dosomething::invoke
. method public hidebysig newslot virtual
Instance class [Mscorlib]system.iasyncresult BeginInvoke (
Int32 times,
class [Mscorlib]system.asynccallback Callback,
Object ' object '
) runtime managed
{
}//End of method Dosomething::begininvoke
. method public hidebysig newslot virtual
instance void EndInvoke (
class [Mscorlib]system.iasyncresult Result
) runtime managed
{
}//End of method Dosomething::endinvoke
}//End of class delegates. DoSomething
Two. The delegate is defined, of course, to use it to see how the delegate is used:
There are three types of delegates in. NET, respectively, methods, anonymous methods, and lambda expressions; we look at how the delegate is used in the form of a method definition
Copy Code code as follows:
Using System;
Namespace delegates
{
public delegate void dosomething (int times);
Class Program
{
static void Main (string[] args)
{
Declaring a delegate variable and assigning a value to a delegate variable
DoSomething @do = DoA;
You can use the + number or + + to add a method to the delegate
@do + + DoB;
The method in the delegate is executed sequentially as the delegate is executed, in the order in which the delegate is added
@do (1);
You can also remove a method from a delegate by-number or =
@do-= DoA;
@do (2);
@do-= DoB;
After all the methods in the delegate are removed, the delegate is still callable, but nothing is done
@do (3);
Console.read ();
}
Define a method that delegates the same parameters and return values
static void DoA (int times)
{
Console.WriteLine ("Do A {0}", times);
}
Define a method that delegates the same parameters and return values
static void DoB (int times)
{
Console.WriteLine ("Do B {0}", times);
}
}
}
As in the main method in the code, we first define the variable @do of the delegate dosomething, and assign the DOA method directly to the delegate variable; then we add another method to this delegate using the + = symbol or the + number, and of course you can use-or-= Remove the method from the delegate.
A delegate is more powerful than a C + + method pointer in that it can hold more than one method, or you can add or remove methods from the list of methods by performing the-+ + action.
There are several issues that require our attention when performing delegate addition and subtraction operations:
1. The wording of the Entrustment declaration
The delegate declaration can be written in the following notation
Copy Code code as follows:
This is actually a short form of writing, and we know that this is not allowed in. NET 1.x and is not permitted until. NET 2.0, which must be written in. NET 1.x
Copy Code code as follows:
DoSomething @do = new DoSomething (DoA);
We're going to give @do DOA plus DOB when we declare it.
Copy Code code as follows:
DoSomething @do = DoA + DoB;
That's not going to work, the compiler's not doing it, you must use the. Net 1.x notation
Copy Code code as follows:
DoSomething @do = new DoSomething (DoA) + new dosomething (DoB);
2. What happens when you subtract from a delegate the way it does not exist in the delegate?
Please see the following code:
Copy Code code as follows:
DoSomething @do = DoA;
@do-= DoB;
First line of code my life. @do and gives it to DOA; the second line of code I tried to subtract from the @do the Dob,dob does not exist in the @do list of methods, so what happens? First, the compiler does not complain, the program can be normal compilation, execution code found that the program can be normal execution, invoke @do delegate when the correct execution of the DOA method; NET containment of our programmers make mistakes, we subtract from the delegate variable in a delegate does not contain the method, the error will not be normal execution.
3. To the delegate to do subtraction, all the delegates have been reduced, what will happen? Look at the following code
Copy Code code as follows:
DoSomething @do = new DoSomething (DoA) + new dosomething (DoB);
@do-= DoA;
@do-= DoB;
@do (1);
Such code can be compiled successfully, but will be reported at runtime NullReferenceException; This is clearly not what we want, so pay special attention to the delegate when you subtract.
Copy Code code as follows:
<span style= "Text-decoration:line-through;" >public delegate void DoIt (string task);
Class Test
{
static void Main (string[] args)
{
Doit declares that it is legal to give a broader approach to a parameter
DoIt DoIt = new DoIt (DOITIMPL);
DoIt ("Hello");
}
Is broader than the parameters in the delegate definition, the string type can be implicitly converted to an object
static void Doitimpl (object Task)
{
Console.WriteLine ("Doitimpl {0}", Task);
}
}
</span>