Tuesday, September 14, 2010

C # in the delegate and event



Delegate and event. NetFramework application is very broad, however, a better understanding of the commission and a lot of contact with C # events on time is not long people is not easy. They are like a sill children, people over this threshold, that is really too easy, not the past, people and events every time to see the commission felt heart writings to be finished and cut uncomfortable. This article, I will describe two examples of what is the greatest depth and commissioned, why use the delegate, the event's origins,. Net in the delegate and event, delegate and event on the Observer design pattern of meaning, on their intermediate code are also discussed.

The method as a method parameter

Let us no matter how convoluted this title, and regardless of whether the commission is something, look at the following two simple methods, which is nothing but output a greeting on the screen the words:

The following is quoted fragment:

public void GreetPeople (string name) (

/ / Do some extra things, such as initialization of the class, here a little

EnglishGreeting (name);

)

public void EnglishGreeting (string name) (

Console.WriteLine ("Morning," + name);

)

Whether it's two methods have no practical significance. GreetPeople to say hello to someone, when we pass the name on behalf of a person's name parameters, such as "Jimmy", entered, in this method is called EnglishGreeting method, again passing name parameter, EnglishGreeting is used to screen output "Morning, Jimmy".

Now assume that the program needs to globalization, Oh, well, I'm Chinese, I do not understand the "Morning" What does it mean, how do? Well, we are introducing a Chinese version of the greetings method:

The following is quoted fragment:

public void ChineseGreeting (string name) (

Console.WriteLine ("Good morning," + name);

)

This time, GreetPeople also need to modify it to read, or else how to determine which version of Greeting in the end with regards to methods appropriate? Doing this, we'd better define an enumeration as the basis to judge:

The following is quoted fragment:

public enum Language (

English, Chinese

)

public void GreetPeople (string name, Language lang) (

/ / Do some extra things, such as initialization of the class, here a little

swith (lang) (

case Language.English:

EnglishGreeting (name);

break;

case Language.Chinese:

ChineseGreeting (name);

break;

)

)

OK, even though this solves the problem, but I do not say that you can easily think of, this solution scalable to very poor if in the future we need to add Korean and Japanese version, you have to repeatedly modify the enumeration and GreetPeople () method, to adapt to new demands.

In considering new solutions, we look GreetPeople method signature:

The following is quoted fragment:

public void GreetPeople (string name, Language lang)

We only look at string name, here, string is the parameter type, name is the parameter variable name when we assign the string "jimmy" when it representative of "jimmy" the value; when we assign it "on Ground", the It also stands for "on Ground," this value. Then we can approach the body of this name in other operations. Hey, this is nonsense Why, just learning program will know.

If you thought about it, if GreetPeople () method can accept a parameter variable, this variable may represent another way, when we give the variable assignment EnglishGreeting time, it represents EnglsihGreeting () this method; when we assign to it ChineseGreeting time, it represents ChineseGreeting () method. We named this parameter variable MakeGreeting, it can not be assigned as to the name as it was when the call GreetPeople () method when the parameters are assigned to this MakeGreeting on the value of Mody (ChineseGreeting or EnglsihGreeting, etc.)? Then, we in the method body , you can also use other parameters the same as used MakeGreeting. However, MakeGreeting represents a method, it is used and it should be assigned methods (such as ChineseGreeting) is the same, such as:

The following is quoted fragment:

MakeGreeting (name);

Well, have ideas, we now come to change to change GreetPeople () method, then it should look like the:

The following is quoted fragment:

public void GreetPeople (string name, 1 10 11 12 13 14 15 16 17 18 19 2 20 21 22 23 24 25 26 27 28 29 3 30 31 32 4 5 6 7 8 9 MakeGreeting) (

MakeGreeting (name);

)

Took note of 1 10 11 12 13 14 15 16 17 1,819,220,212,223,242,526 2,728,293,303,132,456,789, usually placed in this position should be the parameter type, but so far We just think there should be able to represent method parameters, according to this thinking to rewrite GreetPeople method, now there is a big problem: this represents the method MakeGreeting What should be the type argument?

NOTE: no longer need to enumerate here, since the time of assignment to MakeGreeting dynamically decide which method to use is ChineseGreeting or EnglishGreeting, two methods in this internal, has been the use of "morning" or "good morning" to the distinction.

You should have thought of the wise, and now is the time to entrust the appearance, but about the commission, we look at MakeGreeting parameters can represent ChineseGreeting () and EnglishGreeting () method signature:

The following is quoted fragment:

public void EnglishGreeting (string name)

public void ChineseGreeting (string name)

String name as acceptable types of "true" and "1", but can not accept the true and bool type 1 as type int. MakeGreeting parameter type definition should be able to determine the method MakeGreeting can represent the types further stresses that MakeGreeting can represent the method parameter types and return type.

Thus, commissioned there: it defines the parameters can represent methods MakeGreeting types, that is MakeGreeting parameter type.







Recommended links:



Real player format



Convert quicktime to windows media



avi to mpeg Converter free



Convert flac mp3



No comments:

Post a Comment