Events are certainly one of the most commonly used elements in.NET programming. In both ASP.NET and WinFrom development, form loading, Paint, Init, etc. “Protected void Page_Load(object sender, EventArgs e)” This code should be familiar to everyone. If you’re careful, you’ll notice that a lot of event methods take object sender, eventArgs e. Isn’t that very similar to a delegate?

1. Delegation (also known as delegation in some books)

What is the delegate? The meaning of the name has given us imagination space, you are programming, you are writing an ASP.NET web page, and JS is unfamiliar to you, so you entrust one of your colleagues to help you complete the JS part. That’s delegating, giving someone else what you can’t do. And how do you know who’s going to do it? The name, of course! And in order to distinguish between different people with the same name, therefore, a trait needs to be described.

In C#, delegates are described as Pointers to functions that can be used to call different functions while the program is running. This is the same as if you were delegating JS code to a colleague. If there are two colleagues who can do this, they only need to make the results that meet your needs (like an interface), although they do different processes and produce different results, but can meet your requirements.

1. Simple delegate

So what information does the delegate need to carry? First, it stores the name of the method, along with a list of arguments (method signatures), and the type to return. Delegate string/ return type/processDelegate (int I); That’s the definition of a delegate. The blue section is the key to declare the delegate, the red section is the type to return, the black section is the type name of the delegate, which is similar to the name of a class, and the () section is the parameter section. If you want to use the delegate to do something, then the method must meet the following conditions: 1. The return type of the delegate is the same as the return type of the delegate, in this case a String. 2, Can and can only have one argument, and is of type int. OK, meet the above two conditions and everything will work 🙂

For example: 1 using System; 2 using System.Collections.Generic; 3 using System.Text; 10 /// 11 /// 12 ///
13 public delegate string ProcessDelegate(string s1, string s2); 14 15 class Program 16 {17 static void Main(String [] args) 18 {19 / call method / 20 ProcessDelegate PD = new ProcessDelegate(new Test().Process); 21 Console.WriteLine(pd(“Text1”, “Text2”)); 22} 23} 24 25 public class Test 26 {27 /// 30 /// 30 /// 31 /// 32 ///
33 public string Process(string s1,string s2) 34 { 35 return s1 + s2; 36} 37} 38} Text1Tex2

Generic Delegate

Generic delegates, if the type of the parameter is uncertain, such as code rewritten to: using System; using System.Collections.Generic; using System.Text;

namespace TestApp

{

/ / / < summary > / / / / / / < summary > / / / < param name = "s1" > < param > / / / < param name = "s2" > < param > / / / < returns > < / returns > public delegate string ProcessDelegate<T,S>(T s1, S s2); Class Program {static void Main(String [] args) {/* Call method */ ProcessDelegate< String,int> pd = new ProcessDelegate<string,int>(new Test().Process); Console.WriteLine(pd("Text1", 100)); }} public class Test {/ / / < summary > / / / method / / / < summary > / / / < param name = "s1" > < param > / / / < param name = "s2" > < param > /// <returns></returns> public string Process(string s1,int s2) { return s1 + s2; }}

}

The output is Text1100

The details of generics are outside the scope of this article, so I won’t go into them here.