Delegate in C# is similar to pointers to functions in C or C++. Delegate is a reference type variable that contains a reference to a method. References can be changed at runtime.
Delegate is especially used to implement event and callback methods. All delegates are derived from classes.
Delegate
The delegation statement determines the methods that can be cited by the delegation. The delegate can point to a method with the same label as it.
For example, suppose there is a delegation:
public delegate int MyDelegate (string s);
The above delegate can be used to refer to any method with a single string parameter and return an int type variable.
The syntax for declaring a delegation is as follows:
delegate <return type> <delegate-name> <parameter list>
Instantiated delegate (Delegate)
Once the delegate type is declared, the delegate object must be created using the new keyword and is related to a specific method. When a delegate is created, the parameters passed to the new statement are written like a method call, but without parameters. For example:
public delegate void printString(string s); ... printString ps1 = new printString(WriteToScreen); printString ps2 = new printString(WriteToFile);
The following example demonstrates the declaration, instantiation, and use of a delegate that can be used to reference a method with an integer parameter and return an integer value.
using System; delegate int NumberChanger(int n); namespace DelegateAppl { class TestDelegate { static int num = 10; public static int AddNum(int p) { num += p; return num; } public static int MultNum(int q) { num *= q; return num; } public static int getNum() { return num; } static void Main(string[] args) { // Create a delegate instance NumberChanger nc1 = new NumberChanger(AddNum); NumberChanger nc2 = new NumberChanger(MultNum); // Call method using delegate object nc1(25); ("Value of Num: {0}", getNum()); nc2(5); ("Value of Num: {0}", getNum()); (); } } }
When the above code is compiled and executed, it produces the following results:
Value of Num: 35
Value of Num: 175
Multicasting of a Delegate
Delegate objects can be merged using the "+" operator. A merge delegate calls the two delegates it merges. Only delegates of the same type can be merged. The "-" operator can be used to remove component delegates from merged delegates.
Using this useful feature of delegate, you can create a list of calls to the methods to be called when the delegate is called. This is called delegated multicasting, also known as multicasting. The following program demonstrates the delegated multicast:
using System; delegate int NumberChanger(int n); namespace DelegateAppl { class TestDelegate { static int num = 10; public static int AddNum(int p) { num += p; return num; } public static int MultNum(int q) { num *= q; return num; } public static int getNum() { return num; } static void Main(string[] args) { // Create a delegate instance NumberChanger nc; NumberChanger nc1 = new NumberChanger(AddNum); NumberChanger nc2 = new NumberChanger(MultNum); nc = nc1; nc += nc2; // Call multicast nc(5); ("Value of Num: {0}", getNum()); (); } } }
When the above code is compiled and executed, it produces the following results:
Value of Num: 75
Use of Delegate
The following example demonstrates the usage of delegates. The delegate printString can be used to refer to a method with a string as input and does not return anything.
We use this delegate to call two methods, the first one prints the string to the console, and the second one prints the string to the file:
using System; using ; namespace DelegateAppl { class PrintString { static FileStream fs; static StreamWriter sw; // Declaration of Entrustment public delegate void printString(string s); // This method prints to the console public static void WriteToScreen(string str) { ("The String is: {0}", str); } // This method prints to the file public static void WriteToFile(string s) { fs = new FileStream("c:\\", , ); sw = new StreamWriter(fs); (s); (); (); (); } // This method takes the delegate as a parameter and calls the method using it public static void sendString(printString ps) { ps("Hello World"); } static void Main(string[] args) { printString ps1 = new printString(WriteToScreen); printString ps2 = new printString(WriteToFile); sendString(ps1); sendString(ps2); (); } } }
When the above code is compiled and executed, it produces the following results:
The String is: Hello World
The above is a brief analysis of the detailed content of C# delegation (Delegate). For more information about C# delegation (Delegate), please pay attention to my other related articles!