Delegate (dekegate) is a type of dynamic invocation method that is the same as a class, interface, and array, and is a reference type that can be used to implement multicast (MulticastDelegate).
Multicast (multicastdelegate): You can use " +" "-" to Merge and remove multiple delegate objects, you can use "+ =" to add a delegate object,"-=" Remove the delegate object.
Characteristics of the Commission:
1) Delegates are similar to C + + function pointers, but unlike pointers, delegates are fully object-oriented and secure data types;
2) The delegate allows the method to be passed as a parameter;
3) The delegate can be used to define the callback method;
4) A delegate can link multiple methods together. In this way, multiple time handlers can be started at the same time when triggered.
Declaration of the delegate:
[ access modifier ] Delegate Returns a value type delegate name ([ parameter list ]);
Public Delegate int Calculate (intint y);
Instantiation of a delegate:
Delegate type delegate variable name = New delegate constructor ( method name to be referenced by the delegate )
intMultiply (intXinty) { returnX *y;}intADD (intXinty) { returnX +y;} Calculate a=NewCalculate (Multiply); Calculate b=NewCalculate (ADD); Calculate C=NewCalculate (); C= a + b;//Combination CallC+=NewCalculate (ADD);//To add a delegate objectC-= A;//remove a Delegate object
Where A,b is a delegate-type object.
Because instantiating a delegate actually creates an object, the delegate object can participate in assignment operations and even pass as method parameters.
Using delegates:
New Calculate (ADD); int result = Calc (3,6);
Here Calc is equivalent to calling the Add method.
Use anonymous methods:
starting with C # 2.0 , the concept of anonymous methods is introduced, allowing code blocks to be passed as parameters, avoiding the separate definition of methods.
Delegate type delegate variable name = Delegate ([ parameter list ]){ code block };
usingSystem;Delegate voidDintx);//declaring a delegateclassc{ Public Static voidM1 (inti) {Console.WriteLine ("C.M1:"+i); } Public Static voidM2 (inti) {Console.WriteLine ("c.m2:"+i); } Public voidM3 (inti) {Console.WriteLine ("c.m3:"+i); }}classtest{Static voidMain () {D CD1=NewD (C.M1); CD1 (-1);//Call M1Console.WriteLine (); D CD2=NULL; CD2+=NewD (C.M2); CD2 (-2);//Call M2Console.WriteLine (); D CD3= CD1 +CD2; CD3 (Ten);//Call M1 then M2Console.WriteLine (); CD3+=CD1; CD3 ( -);//Call M1, M2 and then M1Console.WriteLine (); C C=NewC ();//the judgment condition of the same or not: whether the function body is the sameD CD4 =NewD (C.M3); CD3+=CD4; CD3 ( -);//Call M1, M2, M1 and then M3Console.WriteLine (); CD3-= CD1;//Remove Last M1CD3 ( +);//Call M1, M2 and then M3Console.WriteLine (); CD3-=CD4; CD3 ( -);//Call M1 then M2Console.WriteLine (); CD3-=CD2; CD3 ( -);//Call M1Console.WriteLine (); CD3-= CD2;//impossible removal is benignCD3 ( -);//Call M1Console.WriteLine (); CD3-= CD1;//invocation list is emptyConsole.WriteLine (CD3 = =NULL ); //CD3 (70); //System.NullReferenceException ThrownCD3-= CD1;//Impossible RemovalConsole.WriteLine (CD3 = =NULL ); Console.readkey (true); }}
Operation Result:
Delegate (delegate)