Delegation and events are widely used in C #, especially in form programming. Many operations must be handled and transmitted through delegation and events. Here we will explain in detail the use and causes of delegation and events, so that we can better understand the code.
Use of delegation
An event is a mechanism that is implemented by a delegate. Therefore, you can understand the delegate and better understand the event. Let's take a look at the following small example.
This is the code that describes programmers in different languages:
For example, if C # is used as a programmer, The programername parameter indicates the programmer's name.
Public void csharpprogramer (string programername) {console. writeline (string. Format ("{0} use C # programming! ", Programername ));}
Using Java,
Public void javaprogramer (string programername) {console. writeline (string. Format ("{0}) programming in Java! "));}
So how can we tell which programmer is this? It is represented by an enumeration,
public enum ProgramLanguage { CSharp, C, Java }
The Code is as follows:
Public class programerdescription {// 01 public void csharpprogramer (string programername) {console. writeline (string. Format ("{0} use C # programming! ", Programername);} public void cprogramer (string programername) {console. writeline (string. Format (" {0} programming in C language! ", Programername);} public void javaprogramer (string programername) {console. writeline (string. Format (" {0} programming in Java! ");} // 02 public void description (string programername, programlanguage type) {Switch (type) {Case programlanguage. CSHARP: csharpprogramer (programername); break; Case programlanguage. c: cprogramer (programername); break; Case programlanguage. java: javaprogramer (programername); break; default: console. writeline ("a kind of new language that you never heard. "); break ;}}public Enum programlanguage {CSHARP, C, Java}
Call:
Programerdescription PM = new programerdescription (); PM. Description ("Yang youshan", programlanguage. CSHARP );
Then, output "Yang youshan uses C # programming! ";
In this way, a simple implementation of programmer description is preliminarily completed. The description method here is to use an enumeration type to distinguish different languages. In this way, the maintainability is relatively poor, because there are many programming languages (C ++, PHP, etc.). If you want to add a language, you need to add a corresponding method, then modify the enumeration type of programlanguage. Adding a method has little impact on the original program, because it can be implemented using a division class, but modifying enumeration is not suitable. This does not conform to the open and closed principle of the object-oriented design idea.
In this case, the description method does not need to be enumerated. How does the description method tell the programmer which method (csharpprogramer, cprogramer, and javaprogramer) to be called.
Public void description (string programername, a parameter of a certain type)
{
// Call the corresponding method
}
A type here is used to identify the type of the method. In other words, it is best to directly pass the method I want to call as a parameter here. For example, I want to call the cprogramer method to describe C programmers,
Programerdescription PM = new programerdescription ();
PM. Description ("D. M. Ritchie", cprogramer );
According to this idea, how does one represent "a certain type of parameter" in Public String description (string programername, a certain type of parameter?
Of course, we just thought of entrusting delegate in C. A delegate is a data structure that references static methods, class instances, and class instance methods. Use a delegate to complete the above Code:
Public class programdescriptiondelegate {// delegate, indicating the method Public Delegate void descriptiondelegate (string programername); // 01 public void csharpprogramer (string programername) {console. writeline (string. format ("{0} use C # programming! ", Programername);} public void cprogramer (string programername) {console. writeline (string. Format (" {0} programming in C language! ", Programername);} public void javaprogramer (string programername) {console. writeline (string. Format (" {0} programming in Java! ");} // 02 public void description (string programername, descriptiondelegate description) {description (programername );}}
Call:
Programdescriptiondelegate PM = new programdescriptiondelegate (); PM. Description ("Yang youshan", PM. csharpprogramer );
The result is the same as the initial code.
What is delegation? Msdn explains this as follows: delegate is a type that defines the method signature. It can be associated with any method that has a compatible signature and can call the method through Delegate. Delegate is used to pass methods as parameters to other methods. A time handler is a method called by Delegate.
Take a closer look at the delegated code,
Public void description (string programername, descriptiondelegate description ). When calling,
Descriptiondelegate description refers to PM. csharpprogramer, that is, description = PM. csharpprogramer. In fact, we can also call:
// Method and delegate bind programdescriptiondelegate PM = new programdescriptiondelegate (); yys. csharpstudy. window. sdelegate. programdescriptiondelegate. descriptiondelegate csharpdelegate = PM. csharpprogramer; yys. csharpstudy. window. sdelegate. programdescriptiondelegate. descriptiondelegate cdelegate = PM. cprogramer; PM. description ("Yang youshan", csharpdelegate); PM. description ("D. m. ritchie ", cdelegate );
Result:
Make the following changes:
Programdescriptiondelegate PM = new programdescriptiondelegate (); yys. csharpstudy. window. sdelegate. programdescriptiondelegate. descriptiondelegate pdelegate = NULL; pdelegate + = PM. csharpprogramer; pdelegate + = PM. cprogramer; PM. description ("Yang youshan", pdelegate );
Result:
Here, the PM. csharpprogramer is executed first, and then the PM. cprogramer method is executed.
It can be seen that the delegate can not only use methods as parameters, but also bind methods, and you can also bind multiple methods.
Use the + = binding method, and use-= to cancel the cancellation. The preceding call can be modified as follows:
Programdescriptiondelegate PM = new programdescriptiondelegate (); yys. csharpstudy. window. sdelegate. programdescriptiondelegate. descriptiondelegate pdelegate = NULL; pdelegate + = PM. csharpprogramer; pdelegate + = PM. cprogramer; pdelegate-= PM. cprogramer; PM. description ("Yang youshan", pdelegate );
In this way, only PM. csharpprogramer is called.
Code download: http://download.csdn.net/detail/yysyangyangyangshan/4840929