Entrusting (1) Understanding entrusting and entrusting
Delegate is. net. net 1. the x version contains many applications in the project development process, but many people have never understood it very clearly (including me, although I have been developing for many years and may have used delegation in many places without knowing it), I will organize a series of articles to summarize and deepen my impression on delegation.
What is Delegation
Two points can be summarized:
1. The delegate is a class, which can be proved by checking the IL code. For example.
2. The delegate contains multiple methods with the same signature and same return value type (or only one ).
We can get some information. 1. The delegate is a class ). 2. The delegate inherits from the System. MulticastDelegate type.
Why use delegate
In other words, it is easier to understand the benefits of using delegation.
The following uses a small example to demonstrate the benefits of using delegation. The requirement is that the Chinese and English greetings are printed on the name and console respectively.
Do not use delegation:
1 namespace DelegateDemo 2 {3 class Program 4 {5 static void Main (string [] args) 6 {7 // input the name, the console prints greetings 8 Hello ("wheat", Language. english); 9 Hello ("mcgrady", Language. 10 11 Console. readKey (); 12} 13 14 // <summary> 15 // greeting 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 // Chinese greetings 33 /// </summary> 34 /// <param name = "name"> </ param> 35 private static void ChineseHello (string name) 36 {37 Console. writeLine (string. format ("Hello, {0}", name )); 38} 39 40 // <summary> 41 // greetings in English: 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 // language type 53 // </summary> 54 public enum Language55 {56 English, 57 Chinese58} 59}
When you do not use delegation, you must use an enumeration Language to indicate whether to use Chinese greetings or English greetings, and determine if... else.
It may be much easier to directly pass the method. Let's take a look at it.
Delegated use:
1 namespace DelegateDemo 2 {3 // declare delegate 4 delegate void MyDel (string name); 5 6 class Program 7 {8 static void Main (string [] args) 9 {10 // by entering the name, the console prints the Chinese and English greetings 11 Hello ("wheat", ChineseHello); 12 Hello ("mcgrady", EnglishHello ); 13 14 Console. readKey (); 15} 16 17 /// <summary> 18 // greeting 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 // Chinese greeting 29 /// </summary> 30 /// <param name = "name"> </param> 31 private static void ChineseHello (string name) 32 {33 Console. writeLine (string. format ("Hello, {0}", name )); 34} 35 36 // <summary> 37 // greetings in English 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}
Compared with not using delegation, we can see that:
1. The judgment of if... else... is omitted.
2. The program is more object-oriented and more scalable. If the Japanese and German versions are available later, you can directly input the German name and the German Hello method.
Of course, this is just an example to illustrate the benefits of using delegation. In fact, delegation is used in many places, such as the button click events of winform and webform, func AND Action delegate in Linq.
Delegated application scenarios
1. click the winform and webform buttons.
2. Func AND Action delegate in Linq.
Use XMind To summarize: