6.6 Delegates
A delegate (delegate) is a type that can store a reference as a function. This sounds pretty tricky, but its mechanism is very simple. The most important use of a delegate can be explained later in this book, with events and event handling, but many of the contents of the delegate are also described here. A delegate's declaration is very similar to a function, but without the body of the function, and uses the delegate keyword. The declaration of a delegate specifies a return type and a list of parameters.
After a delegate is defined, you can declare a variable of that delegate type. The variable is then initialized to a function reference that has the same return type and argument list as the delegate. After that, you can call the function with a delegate variable, just as the variable is a function.
Once you have a variable that references a function, you can also perform an operation that cannot be done in any other way. For example, you can pass a delegate variable as an argument to a function so that the function can invoke any function it references by using a delegate, and you don't need to know which function to call before you run it . The following example uses a delegate to access one of two functions.
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespacech06ex05{classProgram {Delegate DoubleProcessdelegate (DoublePARAM1,DoublePARAM2);//Define Delegate Static DoubleMultiply (DoublePARAM1,Doubleparam2) { returnPARAM1 *param2; } Static DoubleDivide (DoublePARAM1,Doubleparam2) { returnPARAM1/param2; } Static voidMain (string[] args) {processdelegate process; //declaring delegate variablesConsole.WriteLine ("Enter 2 numbers separated with a comma:"); stringinput =Console.ReadLine (); intCommapos = input. IndexOf (','); Doubleparam1 = convert.todouble (input. Substring (0, Commapos)); Doubleparam2 = convert.todouble (input. Substring (Commapos +1, input. Length-commapos-1)); Console.WriteLine ("Enter M to multiply or D to divide:"); Input=Console.ReadLine (); if("M"==input) {Process=NewProcessdelegate (Multiply);//Initialize the delegate variable, note the parameter } Else{Process=NewProcessdelegate (Divide);//Initialize the delegate variable, note the parameter} Console.WriteLine ("Result: {0}", Process (param1, param2)); Console.readkey (); } }}
The results of the operation are as follows:
Initialize the process delegate variable:
if " M " ) new processdelegate (Multiply); Else New Processdelegate (Divide);
Note that this parameter does not match the argument of the delegate type or the target function, which is a unique syntax for delegate assignment, and the parameter is the function name to be used without parentheses.
In fact, you can use a slightly simpler syntax here:
if " M " ) =Else= Divide;
The compiler will find that the delegate type of the process variable matches the signature of the two function, thus automatically generating a delegate. You can decide which syntax to use, but some people prefer to use a longer version, because it's easier to see what happens at a glance.
Here, the delegate variable is treated as a function name. But unlike functions, we can do more with this variable, for example, by passing it to a function by argument, a simple example of this function is as follows:
Static void executefunction (processdelegate process) { process (2.23.3); }
Like selecting a "plug-in" to be used, passing them to the function delegate allows you to control the execution of the function. For example, a function would sort an array of strings alphabetically by letter. There are several different ways to sort a list, and their performance depends on the list attributes to be sorted. Delegates can be used to pass a sort algorithm function delegate to a sort function, specifying the method to use .
(original) C # learning note 06--function 06--delegate