Home >Backend Development >C#.Net Tutorial >What is delegation? Summary of important feature delegation in .net

What is delegation? Summary of important feature delegation in .net

零下一度
零下一度Original
2017-06-24 09:41:002983browse

Delegation is a very important feature in .net. It has been included since .net 1.x version. Delegation is widely used in the project development process, but many students have not understood it very clearly (including Although I have been doing development for many years, I may have used delegation unknowingly in many places, but I am still not very clear about it), so I will compile a series of articles to summarize and deepen my impression of delegation.

What is a delegate

can be summarized in two points:

1, the most popular understanding is, A safe 'function pointer'.

2. Essentially, a delegate is a class (can be proved through IL as shown below), and the delegate contains multiple methods with the same method signature and the same return value.

Delegated class diagram:

From the above picture We can get some information, 1. The delegate is a class. 2. The delegate inherits from the System.MulticastDelegate type.

Why use delegation

A more popular statement is what benefits can be brought by using delegation.

The following is a small example to demonstrate the benefits of using delegation. The requirement is that by entering the name, the console prints Chinese and English greetings respectively.

Do not use delegation:

 1 namespace DelegateDemo 2 { 3     class Program 4     { 5         static void Main(string[] args) 6         { 7             //通过输入的name,控制台分别打印中文和英文的问候语 8             Hello("麦子", Language.Chinese); 9             Hello("mcgrady", Language.English);10 11             Console.ReadKey();12         }13 14         /// <summary>15         /// 问候16         /// </summary>17         /// <param name="name"></param>18         /// <param name="lang"></param>19         private static void Hello(string name, Language lang)20         {21             if (lang == Language.Chinese)22             {23                 ChineseHello(name);24             }25             if (lang == Language.English)26             {27                 EnglishHello(name);28             }29         }30 31         /// <summary>32         /// 中文问候33         /// </summary>34         /// <param name="name"></param>35         private static void ChineseHello(string name)36         {37             Console.WriteLine(string.Format("你好,{0}", name));38         }39 40         /// <summary>41         /// 英文问候42         /// </summary>43         /// <param name="name"></param>44         private static void EnglishHello(string name)45         {46             Console.WriteLine(string.Format("hello,{0}", name));47         }48 49     }50 51     /// <summary>52     /// 语言类型53     /// </summary>54     public enum Language55     {56         English,57         Chinese58     }59 }

When not using delegation, you need to use an enumeration Language to indicate use Chinese greeting or English greeting, and it also needs to be judged if...else....

It may be much simpler when the method can be passed directly. Let’s take a look.

Use delegation:

 1 namespace DelegateDemo 2 { 3     //声明委托 4     delegate void MyDel(string name); 5  6     class Program 7     { 8         static void Main(string[] args) 9         {10             //通过输入的name,控制台分别打印中文和英文的问候语11             Hello("麦子", ChineseHello);12             Hello("mcgrady", EnglishHello);13 14             Console.ReadKey();15         }16 17         /// <summary>18         /// 问候19         /// </summary>20         /// <param name="name"></param>21         /// <param name="myDel"></param>22         private static void Hello(string name, MyDel myDel)23         {24             myDel(name);25         }26 27         /// <summary>28         /// 中文问候29         /// </summary>30         /// <param name="name"></param>31         private static void ChineseHello(string name)32         {33             Console.WriteLine(string.Format("你好,{0}", name));34         }35 36         /// <summary>37         /// 英文问候38         /// </summary>39         /// <param name="name"></param>40         private static void EnglishHello(string name)41         {42             Console.WriteLine(string.Format("hello,{0}", name));43         }44 45     }46 }

So to sum up, the main benefits of delegation are There are the following points:

1. It eliminates a lot of if...else...or switch... judgments, making the program more object-oriented.

2, decoupling, the program is easier to expand. Example: Tracy.Proxy interface proxy component, records xml logs and performance logs.

Of course, this is just an example to illustrate the benefits of using delegation. In fact, delegation is used in many places, such as button click events in winform and webform, Func and Action delegation in Linq, etc. .

Application scenarios of delegation

1, button click events of winform and webform.

2, Func and Action delegates in Linq.

3, Tracy.Proxy interface proxy component

Use XMind to summarize:

The above is the detailed content of What is delegation? Summary of important feature delegation in .net. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn