Refuse the "cold" word! Entrustment in C# for beginners

Posted by kidbrax on Thu, 16 May 2019 20:07:02 +0200

One day, you wrote a lot of constructors with "formal parameters" (that is, methods, synonyms), and you need to pass the same "argument" to these constructors, then you call a function and give the same "argument". That's the end of the day.

A few days later, you will call so many functions again, and you will call one function honestly?! Smart you, you will definitely think: really TM trouble! Is there a way to do it once and for all?

What you need is -- "delegation"

 

Let's start with a simple example of a VIP coming to a luxury hotel, where the boss gives notice and his two assistants (ass1 and ass2) inform the waitress and the cook, respectively, what they should do.

 1 class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5             //Delegation is a "class" and needs to be "instantiated",Let " ass1"Notification " Waiteress". 
 6             Convey_Dele ass1 = new Convey_Dele(Waiteress);
 7 
 8             //Let " ass2"Notification " Cooker"
 9             Convey_Dele ass2 = new Convey_Dele(Cooker);
10 
11             //"boss"Notify two assistants“ ass1 and ass2",The term is "Merger delegation (or "multi-channel broadcasting delegation")"
12             Convey_Dele boss = new Convey_Dele(ass1 + ass2);
13 
14             //"boss"Utterance
15             boss("A VIP is coming!");                                      
16 
17             Console.ReadKey();
18         }
19 
20         //Declare a statement called“ Convey_Dele"The entrustment of“ string command"
21         public delegate void Convey_Dele(string command);
22 
23         //Construct a name called“ Waiteress"Function,The parameters of the constructor must be consistent with the delegated parameters.
24         public static void Waiteress(string command)                   
25         {
26             //"Waiteress"What to do     
27             Console.WriteLine("The waitress was informed:" + command + "So she's going to meet the guests.");       
28         }
29 
30         public static void Cooker(string command)
31         {
32             Console.WriteLine("The chef was also notified:" + command + "So he went to prepare the meal.");
33         }
34     }

The results after operation are as follows:

In this way, the boss gives any instructions, the subordinates should do what to do, the boss does not need to know the specific actions of the subordinates, as long as they do well, it can be completed.

 

Smart you think, can you simplify and diversify your code? Sure. The above code can be changed to:

 1 class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5             Convey_Dele ass1, ass2;
 6 
 7             //It's too troublesome to write another function to instantiate, so I'll write the function together directly here. Terminology "Anonymous function","delegate"Key word
 8             ass1 = delegate (string command) { Console.WriteLine("The waitress was informed:" + command + "So she's going to meet the guests."); };
 9 
10             //Anonymous functions are too cumbersome to write.“ delegate",Change to a symbol, terminology“Lambda expression"
11             ass2 = (string command) => { Console.WriteLine("The chef was also notified:" + command + "So he went to prepare the meal."); };
12 
13             //Equate to ass1 = ass1 + ass2,The basic "addition" operator and the "subtraction" operator can also be used.~ For example: if ass1 = ass1 + ass2,that ass1 -= ass1 Equate to ass1=ass2
14             ass1 += ass2;     
15 
16             ass1("A VIP is coming!");
17 
18             Console.ReadKey();
19         }
20 
21         public delegate void Convey_Dele(string command);
22     }

The results after operation are identical to those before simplification. Such a simplified way of writing still has a strong "readability", "anonymous function" and "Lambda expression" have a simpler way of writing, as long as it is a formal parameter, it can also omit even the formal parameter, so that we can know this thing.

In this way, we can flexibly use delegation to simplify the code and the tedious work of "moving bricks" when calling many functions.

 

Well, the content entrusted by the beginners is so much to talk about first. I am also learning while understanding. There are many shortcomings in blogging. I would like to ask the vast number of kindergarten friends to point out that I am not stingy to teach!

Topics: C# Lambda