Delegates and events are used in C # a lot, especially when it comes to form programming, and many operations are handled and passed through delegates and events. Here we explain in detail the usage and reason of the delegate and the event, so that we can write the code more clearly.
Use of Delegates
An event is a mechanism in which events are implemented by means of a delegate, so that when you see the delegate, you can better understand the event. Let's look at a small example below.
This is the code for programmers who describe different languages:
For example, programmers using C # We describe this, where the Programername parameter represents the programmer's name.
public void Csharpprogramer (string programername) { Console.WriteLine (string. Format ("{0} Programming with C #!", Programername)); }
With Java,
public void Javaprogramer (string programername) { Console.WriteLine (string. Format ("{0} programming with the Java language!"); }
So many languages, how do you tell which programmer this is? Use an enumeration to indicate that a
Public enum Programlanguage { CSharp, C, Java }
Then a complete description of the code is as follows:
public class Programerdescription {//01 public void Csharpprogramer (string programername) { Console.WriteLine (String. Format ("{0} Programming with C #!", Programername)); The public void Cprogramer (string programername) {Console.WriteLine (string. Format ("{0} Programming with C language!", Programername)); The public void Javaprogramer (string programername) {Console.WriteLine (string. Format ("{0} programming with the Java language!"); }//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 of."); Break }}} public enum Programlanguage {CSharp, C, Java}
Call:
programerdescription pm = new Programerdescription (); Pm. Description ("Yang Youshan", programlanguage.csharp);
At this point, output "Yang Youshan using C # programming!";
This completes a simple implementation of the programmer's description. The description method here is to distinguish different languages by an enumeration type. So it's maintainability is poor, because there are many programming languages (c++,php, etc.), if you want to add a language to add a corresponding method, then you need to modify the Programlanguage this enumeration type. The increment method has little effect on the original program, because it can be implemented by partial class, and it is inappropriate to modify the enumeration, which does not conform to the open and closed principle of object-oriented design.
So what to do in the description method, that is, without enumerating, how the description method distinguishes the calling method (Csharpprogramer,cprogramer,javaprogramer) from describing the programmer.
public void Description (string programername, a parameter of a type)
{
Call the corresponding method
}
One of the types here is to use it to distinguish which method is the type, in other words, it is better to be here I directly put the method I want to invoke as a parameter to pass in. For example, I'm going to call the Cprogramer method to describe C programmers,
programerdescription pm = new Programerdescription ();
Pm. Description ("D.m.ritchie", Cprogramer);
According to this idea, what does the public string Description (string programername, a parameter of a certain type) say about "a certain type of parameter"?
Of course we think of the delegate of the delegate in C #. A delegate is a data structure that references a static method or a reference class instance and an instance method of that class. Use the delegate to complete the above code:
public class Programdescriptiondelegate { //delegate, representing method public delegate void Descriptiondelegate (string Programername); csharpprogramer public void (string programername) { Console.WriteLine (string. Format ("{0} Programming with C #!", Programername)); } public void Cprogramer (string programername) { Console.WriteLine (string. Format ("{0} Programming with C language!", Programername)); } public void Javaprogramer (string programername) { Console.WriteLine (string. Format ("{0} programming with the Java language!"); } The 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 code at the beginning.
What is a delegate? MSDN explains this: a delegate is a type that defines a method signature, can be associated with any method that has a compatible signature, and can be invoked through a delegate. Delegates are used to pass methods as parameters to other methods. A time handler is a method that is invoked through a delegate.
Look closely at the code that uses the delegate,
public void Description (string programername, Descriptiondelegate Description). When called,
Descriptiondelegate Description the incoming PM. Csharpprogramer, that is, at this time description=pm. Csharpprogramer. In fact, we can also call this:
Method and delegate Binding 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);
Results:
Make changes again:
programdescriptiondelegate pm = new Programdescriptiondelegate (); Yys. CSharpStudy.Window.SDelegate.ProgramDescriptionDelegate.DescriptionDelegate pdelegate = null; Pdelegate + = pm. Csharpprogramer; Pdelegate + = pm. Cprogramer; Pm. Description ("Yang Youshan", pdelegate);
Results:
The PM is executed first. Csharpprogramer, and then executes the PM. Cprogramer method.
You can see that the delegate can not only use the method as an argument, but also bind the method, and can bind more than one.
Using the + = Binding method, cancellation using-= is canceled, and the above 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);
This will call only pm. Csharpprogramer; a method.
The above is the basic knowledge of C # Basics (20) commissioned (a) content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!