Delegate usage in C

  • 2021-10-24 23:29:26
  • OfStack

Starting today in a series of articles on C #, this article focuses on the use of delegates in C #.

Delegation is actually a data type, and int and string are the same concepts.

If you want to assign a string to a variable, you can declare a variable with string. If you want to assign a method to a variable, what keywords should you use? Of course, the delegate is used, so the variable declared with the delegate can accept a method, and then that variable can be executed like method 1.

Let's start with details:

Look at one piece of code first:


 static void Main(string[] args)
 {
   int i;      // Acceptable 1 Variable of integers 
   string str;    // Acceptable 1 Variable of strings 
 }

Two variables are declared within the Main method: i and str. The familiar code, the variable i indicates that it can accept 1 integer, and the variable str indicates that it can accept 1 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, with which variables declared can accept both an integer and a string.

As follows:


static void Main(string[] args)
{
  MyClass obj = new MyClass();
  obj.i =1;
  obj.str =" I am a string ";
}

Here's the problem: I want a data type that can declare a variable to accept a method.

Since this data type accepts one method, let's first look at what the method looks like:


 public string Method(int m,int n)
 {
   return "";
 }

The main feature of the above Method method is the input parameter data type and the output data type of this method.

Usually we will call various methods. Before calling the method, we will determine the data types of the input parameters and the output data types of the method. As for the body of the method, we usually don't care. The body of the method can be completed by the method programmer.

Therefore, we write a method, and before calling a method, we must specify the input and output data types of the method.

For the above Method method, the input data type is 2 integers and the output is 1 string.

Now we want to declare a variable to accept this method, so the data type of this variable should also specify the input and output data types. Then we should define the data type that matches the input and output 1 of that method.


public class Test
{
  // This data type is acceptable 1 Methods 
  public delegate string MethodDelegate(int i1,int i2);
}

Well, I have declared one data type of the input and output type similar to Method method 1 above: MethodDelegate.

Then we can accept the Mehtod method with this data type. The complete code is as follows:


class Program
{
  // This data type is acceptable 1 Methods 
  public delegate string MethodDelegate(int i1, int i2);

  /// <summary>
  ///  We can use MethodDelegate Declaration 1 Variable to accept the method 
  /// </summary>
  public void Test()
  {
    MethodDelegate IamMethod = Method;  // Put the one below Method Method is assigned to the variable. 
    var result=IamMethod(1, 2);     // Then think of this variable as a method 1 Sample execution can be done. 
  }
  
  public string Method(int m, int n)
  {
    return "";
  }  
}

In line 4 above: We declared a data type with the delegate keyword: this data type and the input and output types of what method we want to accept remain the same.

Then the above MethodDelegate can accept any method with two input parameters of type int and return type string.

Now we find that delegates and int, string are the same concept, 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.


Related articles: