What is a delegate? Summarize the important attribute delegates in. Net

Source: Internet
Author: User
A delegate is a very important feature in. NET, which is included in the. NET 1.x version, which is used by a lot of people in the development of the project, but many students have not understood it very clearly (including me, although it has been developed for many years, it is possible that many places unknowingly also used the Commission, But still not very clear), then I will organize a series of articles to summarize and deepen the impression of the delegation.

What is a delegate

Can be summed up with two points:

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

2, essentially, a delegate is a Class (class) (which can be proved by IL), and the delegate contains multiple methods that have the same return value for the same method signature.

Class diagram for delegates:

From what we can get a few messages, 1, a delegate is a class. 2, the delegate inherits from the System.MulticastDelegate type.

Why do you use delegates

More commonly, the benefits of using a delegate can be.

Here's a small example to illustrate the benefits of using a delegate. The requirement is that by entering the name, the console prints the Chinese and English greetings separately.

Do not use delegates:

 1 namespace Delegatedemo 2 {3 class program 4 {5 static void Main (string[] args) 6 {7 By entering the name, the console prints the greetings in Chinese and English respectively 8 hello ("wheat", Language.chinese); 9 Hello ("McGrady", Language.english), Console.readkey (),}13//<sum MARY&GT;15///Greeting//</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), {Chinesehello (name); 24}25         if (lang = = language.english)-{Englishhello (name); 28}29}30 31 &LT;SUMMARY&GT;32///Chinese greetings//</summary>34//<param name= "name" ></pa   ram>35 private static void Chinesehello (string name) 36      {PNs Console.WriteLine (string.         Format ("Hello, {0}", name),}39//<summary>41//English greetings///</summary>43             <param name= "name" ></param>44 private static void Englishhello (string name) 45 {46 Console.WriteLine (String. Format ("hello,{0}", name),}48}50///<summary>52//language type://</summary>5 4 public enum Language55 {english,57 Chinese58}59}

When you do not use a delegate, you need to use an enumeration language to indicate whether to use Chinese greetings or English greetings, and also to if...else ... The judgment.

It may be a lot easier when you can pass the method directly, and here's a look.

Using delegates:

 1 namespace Delegatedemo 2 {3//Declaration Delegate 4 delegate void Mydel (string name) 5 6 class program 7 {8 static void Main (string[] args) 9 {10//through the input name, the console prints the greetings of the Chinese and English each one hello ("wheat", Chinese Hello); ("McGrady", Englishhello); Console.readkey ();}16//&lt         ; summary>18///Greeting//</summary>20//<param name= "name" ></param>21 <param name= "Mydel" ></param>22 private static void Hello (string name, Mydel Mydel) 23 {2 4 Mydel (name),}26//<summary>28//Chinese greetings///</summary>3 0//<param name= "name" ></param>31 private static void Chinesehello (string name) 32 {3 3 Console.WriteLine (String. Format ("Hello, {0}", name));}35//<summary>37//English Greetings//</summary>39//<param name= "name" ></param>40 private static void Englishhello (string name) Console.WriteLine (String. Format ("hello,{0}", name); 43}44 45}46}

So summing up, the benefits of the Commission mainly have the following points:

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

2, decoupling, the program is easier to expand. Example: Tracy.proxy interface Agent component, logging XML logs and Performance logs.

Of course, this is just an example of the benefits of using a delegate, but there are many places where delegates are used, such as button click events for WinForm and WebForm, Func in LINQ, and action delegates.

Application Scenarios for delegates

button click event for 1,winform and WebForm.

The Func and action delegates in the 2,linq.

3,tracy.proxy Interface Agent Components

Use Xmind to summarize:

Related Article

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.