A delegate is a type of stored function reference that has an important purpose in the handling of events and events
In layman's words, a delegate is a type that can refer to a method, and when a delegate is created, a variable that references the method is created, and then the method can be called, that is, the delegate can invoke the method it refers to.
- Using delegates
The use of delegates requires the following steps:
Defining delegates
Delegate Double Parocessdelegate (double param1,double param2);
The definition of a delegate is very similar to a function, but without the body of the function, and uses the delegate keyword. The delegate definition needs to indicate the delegate name and a return type and a list of parameters
Declaring a variable of a delegate type
Process
Once a delegate is defined, you can declare a variable of that delegate type
Initializing delegate variables
Process =new processdelegate (Multiply);
When initializing a delegate variable, you assign a function (where multiply is the name of a function) to a delegate variable, which needs to have the same return type and argument list as the delegate. C # uses the slightly eccentric syntax above, using the New keyword to create a delegate with the parameter
To reference the required function, this is a unique syntax for delegate assignment, with the function name without parentheses
You can also use another slightly simpler syntax
Process = muiltiply;
With a delegate variable referencing a function, we can call the Muiltiply function with a delegate variable, or you can pass the delegate variable to another function
Process (PARAM1,PARAM2);
Example:
namespacedelegate{ Public Delegate intCall (intNUM1,intNUM2);//First step: Define the delegate type classSimpleMath {//Multiplication Method Public intMultiply (intNUM1,intnum2) { returnNUM1 *num2; } //Division Method Public intDivide (intNUM1,intnum2) { returnNUM1/num2; } } } classTest {Static voidMain (string[] args) {Call objcall;//Step Two: Declare the delegate variable//object of the Math classSimpleMath Objmath =NewSimpleMath (); //Step Three: Initialize the delegate variable and associate the method with the delegate.Objcall =NewCall (objmath.multiply); Objcall+ = Objmath.divide;//add a method to a delegate//Objcall-= objmath.divide;//subtract a method from a delegate//invokes the delegate instance, executes objmath.multiply first, and then executes Objmath.divide intresult = Objcall (5,3); System.Console.WriteLine ("The result is {0}", result); Console.readkey (); } }
Precautions:
- Delegates can call multiple methods, that is, a delegate variable can refer to multiple functions, called multicast
- You can use the + = and-= operators to achieve increased and reduced methods
- A delegate that does not have a return value, and how many methods are executed by reference to the method. A delegate with a return value also executes multiple referenced methods, but the returned value is the return value of the last method
2. Other forms of delegation
Anonymous delegates are more concise to use, not to define a dedicated delegate function to pass the method, but also to better understand the delegate
//Defining Delegates Delegate stringLookme (strings); protected voidLinkButton1_Click (Objectsender, EventArgs e) { //Anonymous DelegateLookme LM =Delegate(stringName) {return "Dear"+ name +", please look me in the eye! "; }; //Anonymous Delegate invocation stringName1 ="Jarod"; Label1.Text=LM (NAME1); }
Action<>,func<>,predicate<> actually they are the shorthand form of the principal agent, through which they can omit the steps to define the delegate
Example
Public Static void Hellowchinese (string Strchinese) { Console.WriteLine ("Good Morning, " + Strchinese); Console.ReadLine (); } Action<string> action = hellowchinese; Action ("Spring. ");
Where action is a generic delegate with no return value, Func is a generic delegate with a return value,predicate<> is a delegate that returns a bool type, and they all have overloaded versions of multiple arguments
3. Various wording of the entrustment
Public Delegate intDelegateprocess (intNUM1,intnum2);//The first form of a notationDelegateprocess process=Newdelegateprocess (Multiply);//The second type of notationDelegateprocess process=Multiply;//The Third Way anonymous delegateDelegateprocess process=Delegate(intAintb) { returnA *b;}//The fourth type of LAMDBA expressionDelegateprocess Process = ((intAintb) =>{returnA *b;});//fifth notation action<t> and func<t>action<int,int> process= ((intAintb) =>{console.writeline (A *b);}); Func<int,int,int> process= ((intAintb) =>{returnA*b;});
C # Delegate Delegate