In the development process, we always hear "entrust" this thing, especially in this college cloud platform. So today we're going to do a little exploring:
Literally, the delegate has the meaning of the agent: a commissioned B to buy things, then B is a agent to carry out the "buy things" behavior. But from a procedural standpoint, a delegate is a type of reference method, and the delegate method can be like any other method. Simply put, a delegate is a data type whose variables can be referenced to a method that meets the requirements, and can be called indirectly by means of a delegate.
Let's look at an example:
without the use of delegates , we want to achieve the "use of English and Chinese two self-introduction" effect, we need to first define two self-introduction methods: English Introduction and Chinese introduction two kinds:
<summary> //The Chinese Speaker: English presentation/// </summary>/ <param name= "name" >the name.</param> public void Englishspeaker (string name) { Console.WriteLine ( string. Format ("Hello My name is {0} and I am 中文版 speaker.\n", name)); } <summary> //The chineses Speaker: How to introduce Chinese ///</summary> public void Chinesespeaker ( String name) { Console.WriteLine ( string. Format ("Hello my name is {0}, I speak Mandarin.") \ n ", name));
After defining two methods, we write the main function and invoke two methods to introduce ourselves in different languages:
///<SU Mmary>//////</summary>//<param name= "name" >string</param> ; <param name= "lang" >enum</param> private static void Say (string name, Language Lang) { Switch (lang) {case Language.Chinese:Program.ChineseSpeaker (name); Break Case Language.English:Program.EnglishSpeaker (name); Break Default:break; } }
in the main function we should use switch......case or if......else to judge whether to introduce ourselves in English or Chinese. However, if we now want to add an Arabic self-introduction, you can add a self-introduced method and increase the invocation by adding case statements in the main function. So the question is, as long as we increase the language we introduce ourselves, will we write a function of the corresponding language? Then there will be N case statements in the main function of our program to enumerate the self-introduction of the N languages we need.
Of course not. Because this not only is not conducive to the expansion and maintenance of the system, but also against the idea of object-oriented. So, we're going to use the delegate approach.
First we define a delegate type Speakdelegate and then use the Say method to see how the delegate variable is used.
<span style= "FONT-SIZE:18PX;" > Private delegate void Speakdelegate (string name);</span>
We then use the delegate variable by modifying the Say method:
private static void Say (String name,speakdelegate speaker{ speaker (name); }
Finally, we can write this directly in the main function of the program:
<span style= "FONT-SIZE:18PX;" > program.say ("Zhang San", <strong><span style= "color: #ff0000;" >ChineseSpeaker</span></strong>); The binding of the transfer function name to the delegate method Program.say ("Zhangsan", englishspeaker); Program.say ("みょうじ", Japanspeaker);
after modification, our
function name has been used as the parameter of the Say method , and this is the thought of the delegate.
Compared to the previous, the main function effectively eliminated the "Switch ... case" judgment statement, the program became simple, also began to conform to the open and closed principle in OOP. And with the technology of the delegate, the parameter is no longer a delegate type, but a delegate type variable. Now we call the say method just to pass a name and a specific implementation function name.
Using the delegate skillfully