Delegates in C # using the _c# tutorial

Source: Internet
Author: User

Starting from today to write a series of articles on C #, this article mainly explains the use of delegates in C #.

A delegate is actually a data type, and int,string is the same concept.

If you want to assign a string to a variable, declare a variable with string. If you want to assign a method to a variable, what keyword does it use? Of course, the delegate is used, so the variable declared by the delegate can accept a method, and then the variable can be executed like a method.

Let's start with a detailed description:

First look at a piece of code:

 static void Main (string[] args)
 {
   int i;      Variable string str that can accept an integer
   ;    can accept a string of variables
 }

2 variables are declared within the Main method: I and Str. Everyone is familiar with the code, variable i indicates that it can accept an integer, and 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 integer public
  string str {get; set;}//Accept String
}

Then we create a data type: MyClass, the variable declared with it 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";
}

Now here's the question: I want a data type to declare a variable to accept a method.

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

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

The most important feature of the method above is the input parameter data type and the data type of the output.

Usually we call various methods, before invoking the method we will determine the data type of the input parameter of the method and the data type of the method output, as for the main body of the method we usually do not care, the main body of the method is done by the method programmer.

So we write a method that calls a method before the explicit method of the input and output data type.

The input data type for the method above is 2 integers, and the output is a string.

Now we want to declare a variable to accept this method, then the data type of the variable should also be explicitly entered, the output of the data type. Then we should define data types that conform to the input and output of that method.

public class Test
{
  //This data type can accept a method public
  delegate string methoddelegate (int i1,int i2);
}

Well, I've already declared a data type for input and output types like the one above method: Methoddelegate.

So 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 use Methoddelegate to declare a variable to accept the method.
  ///</summary> public
  void Test ()
  {
    methoddelegate Iammethod = method;  Assign the following method to the variable.
    var result=iammethod (1, 2);     The variable is then executed as a method.
  }
  
  public string method (int m, int n)
  {return
    "";
  }  
}

On line 4th above: We use the delegate keyword to declare a data type: This data type is consistent with the input and output types we want to accept.

The above methoddelegate can accept input parameters with 2 int types and any method that returns the 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.