C # delegation and events

Source: Internet
Author: User

I have previously written an article on C # delegation and events (see C # delegation and event examples), but I still have some questions from some netizens. So let's take another look at this issue today.

1. Use delegation and events in the console

We all know that C # has the concept of "interface". The so-called "interface" is to define a set of standards and then implement the methods in the implementation class. Therefore, "interface, is the abstraction of a group of classes ". In the same way, we can regard "delegate" as "method abstraction", that is, to define a method template. As for how this method is specific, it is implemented by the method itself.

We know that the biggest benefit of an interface is that it can realize polymorphism. Similarly, "delegate" can realize the polymorphism of methods. When we want to call a specific method, we do not directly call this method, instead, call the delegate. Of course, we must establish an association between the specific method and the delegate.

The following is an example.

First, we define a delegate:

   SaySomething( name);

Well, now that we have defined this rule, we will define the specific method below.

  SayHello( + name +   SayNiceToMeetYou( + name + 

Next, let's look at the event.

Public come;

We have defined an event. This event is "Someone is coming". Note that we use the event keyword when defining it. In addition, we have also added the name of the previously defined "delegate. This means that this event will only deal with "SaySomething", and when this event occurs, I will notify you to pay attention to my "delegates" (then the "delegates" will call the specific methods ).

Let's define a Test method:

 = = +=+=

The following two lines Add the delegate to the event, which means that if this event happens, let me know. "+ =" Can be used to add n delegated instances to an event. Once this event occurs, all these delegated instances will be notified.

The last line triggers an event. Note that we directly use an event name and then use a parameter. This is consistent with the rule defined in "delegate" (that is, must have a string type parameter ).

Last run

  Main(= 

Let's look back at the definition of "Event:

Public come;

The name of the "delegate" has been pointed out here. Therefore, we can directly add the method to the event and omit the instantiation process of the "delegate". Therefore, the above test () the method can be simply written as follows:

 +=+=

The above example does not reflect the advantages of delegation and events. In fact, delegation and events are widely used in C #. For example, when we click a "button, A "Click" event is triggered, and this event notifies the "delegate". In the C # form application, the name of the "delegate" is more standardized and "EventHandler" is used in a unified manner ", the specific format is "void EventHandler (object sender, EventArgs e );". I believe everyone has written the following HelloWorld program:

When you click a button, a dialog box is displayed. How can we implement it? You will certainly say that we double-click the button in the design window to automatically generate a method similar to the following for us:

  button1_Click(

We can see the following code in Form1.Designer. cs:

This. button1.Click + = new System. EventHandler (this. button#click );

We can see that there is a Click event, and then a delegated instance is appended to this event, which is exactly the same as the usage in the console application we mentioned earlier. How is this Click event triggered? We do not care about these system events.

Of course, we can also define our own events and delegation. For example, if I define an event, this event is the name of the output object.

Here we define a ShowName delegate and a btnclick event. In addition, this btnclick event is triggered in the button#click () method. The final result is that when we click the button, a "I Have Been clicked!" is displayed first !!!" Dialog box, and then click OK to bring up another dialog box that displays the button name:

 

Reprinted from: http://ruizhinet.blog.163.com/blog/static/9921382820092801032681/

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.