Start writing a series of articles about C# today. This article mainly explains the use of commissions in C#.
Delegation is actually a data type, and it is the same concept as int and string.
If you want to assign a string to a variable, just declare a variable with string. If you want to assign a method to a variable, what keywords should you use? Of course, it is just a delegate, so a variable declared by the delegate can accept a method, and then that variable can be executed like a method.
The following is a detailed introduction:
Let's look at a piece of code first:
static void Main(string[] args) { int i; // Can accept variables of integers string str; // Can accept variables of a string }
2 variables are declared in Main method: i and str. The code that everyone is familiar with, the variable i indicates that it can accept an integer, and the variable str indicates that it can accept a string.
So if I want to declare a variable that can accept both integers and strings, what data type should I use to declare such a variable?
The answer is simple: use class.
public class MyClass { public int i { get; set; } //Accept integers public string str { get; set; } //Accept string}
Then we create a data type: MyClass, which uses variables declared to accept an integer and a string at the same time.
as follows:
static void Main(string[] args) { MyClass obj = new MyClass(); =1; ="I'm a string"; }
Now the question is: I want a data type that can declare a variable to accept a method.
Since this data type needs to accept a method, let's first look at what the method looks like:
public string Method(int m,int n) { return ""; }
The most important feature of the above Method method is the input parameter data type and output data type of this method.
Usually we will call various methods. Before calling the method, we will determine the data type of the input parameters of the method and the data type of the method output. As for the body of the method, we usually don’t care. The body of the method can be completed by the method programmer.
So we write a method and we must clarify the input and output data types of the method before calling a method.
For the above Method method input data type is 2 integers, the output is a string.
Now we want to declare a variable to accept this method, then the data type of this variable should also be explicit about the input and output data type. Then we should define the data type that matches the input and output of which method.
public class Test { //This data type can accept a method public delegate string MethodDelegate(int i1,int i2); }
OK, I have declared a data type of the same input and output type as the above Method method: MethodDelegate.
Then we can use this data type to accept the Mehtod method, the complete code is as follows:
class Program { //This data type can accept a method public delegate string MethodDelegate(int i1, int i2); /// <summary> /// We can declare a variable to accept the method /// </summary> public void Test() { MethodDelegate IamMethod = Method; // Assign the following Method method to the variable. var result=IamMethod(1, 2); //Then execute this variable as a method. } public string Method(int m, int n) { return ""; } }
In line 4 above: We declare a data type with the delegate keyword: this data type is consistent with the input and output types of the method we want to accept.
Then the above MethodDelegate can accept any method with 2 int type input parameters and the return type is string.
Now we find that delegate is the same concept as int and string, except that int is used to accept integers, string is used to accept strings, and delegate is used to declare a data type acceptance method.