C foundation delegate usage example tutorial

  • 2020-10-07 18:52:03
  • OfStack

This article briefly introduces the use of delegation in C# in the form of examples, which is an important skill to master for further study of C# programming. Now in the form of tutorials to share with you for your reference. The details are as follows:

First, delegates are the most common thing in C#. Like class, enumeration, structure, interface 1, delegate is also a type. Classes are abstractions of objects, while delegates can be thought of as abstractions of functions. 1 delegate represents all functions that have the same argument list and return value. Such as:


delegate int GetCalculatedValueDelegate(int x, int y);

In the above definition, we define a delegate representing a class 1 function whose first argument is an integer x, second argument is an integer y, and whose return value is an integer. Here, for the sake of description, we refer to functions of class 1 as those with the same signature (signature) (note: this signature is not a concept in digital signatures, but simply means that such functions have the same argument list and return value).

Since a delegate is of type 1, it can be used to define parameters, variables, and return values. Variables defined by the delegate are used to hold function entities that have the same signature. It is important to note that, unlike C++, function Pointers in C++ can only hold global or static functions, whereas delegate entities in C# can refer to any function.

Now let's look at an example where we take the delegate defined above, create a delegate entity to refer to the AddCalculator function in the program, and then use the delegate entity directly to get the result of the calculation just as we would use the function itself.


delegate int GetCalculatedValueDelegate(int x, int y); 
 
static int AddCalculator(int x, int y) 
{ 
 return x + y; 
} 
 
static int SubCalculator(int x, int y) 
{ 
 return x - y; 
} 
 
static void Main(string[] args) 
{ 
 GetCalculatedValueDelegate d = AddCalculator; 
 Console.WriteLine(d(10, 20)); 
}

At this point, you can basically understand the meaning of the "delegate". For the above Main function, the AddCalculator function should be called, but it is called by d, that is, the subsequent operation on AddCalculator is handled by d. Xiao Ming was to go to the teacher's office to take the pencil case, because xiao Ming and xiao Wen are good friends, so Xiao Ming will xiao Wen instead of him to take, then xiao Wen became xiao Ming's agent, xiao Ming entrusted xiao Wen to take the pencil case.

Now let's consider the delegate as a parameter. Taking a delegate as an argument abstracts the processing logic of the function itself, leaving it to the caller to decide what kind of logic is ultimately handled. Here are some examples:


delegate int GetCalculatedValueDelegate(int x, int y); 
 
static int AddCalculator(int x, int y) 
{ 
 return x + y; 
}
 
static int SubCalculator(int x, int y) 
{ 
 return x - y; 
} 
 
static int Calculator(GetCalculatedValueDelegate del, int x, int y) 
{ 
 return del(x, y); 
} 
 
static void Main(string[] args) 
{ 
 Console.WriteLine(Calculator(AddCalculator, 10, 20)); 
}

In the above example, the first argument to the Calculator function is a delegate. In fact, what Calculator will do with x and y is not known by itself, and how x and y will be handled is up to GetCalculatedValueDelegate. So in the Main method, we pass the AddCalculator method as a parameter to Calculator, indicating that Calculator is to use the logic of AddCalculator to handle x and y. This is also very graphic: Calculator said: "I don't know what to do with x and y, let del do it!" So they threw x and y to del.

This is a bit like the template method pattern. In the template method pattern, you can leave the mutable parts to be overridden by the subclasses, and the immutable parts to be implemented by the superclasses. Then, with the delegate as a parameter, Calculator can handle the unchanging logic on its own and delegate the "how" to someone else.

Delegate as a parameter is very common in C#. For example, when a thread is created, it takes an ThreadStart or ParameterizedThreadStart delegate as an argument, and when the thread executes, the function referred to by this argument is used as the thread execution body. Another example: List < T > The argument to the Find method of type is also a delegate, leaving the question of "how to find" or "how to find" to the developer. Developers only need to define a function with an argument of T and a return value of Boolean, implement the function body, and pass the function as an argument to the Find method to complete the search for elements in the collection.

The delegate as the return value 1 is typically used in "use different delegates depending on the situation" situations. This is a bit like factory mode, but delegates are still used as return values rather than as arguments.

Delegate concepts include asynchronous calls, generic delegates, anonymous methods, Lambda expressions, events, covariant and contravariant, and so on. Interested readers can check out related articles on this site.


Related articles: