C # delegation and events

Source: Internet
Author: User

In the past two days, I saw the c # delegation and event chapter. The book was very simple and straightforward, but I didn't have the idea of delegation in java. I searched the internet for relevant information written by others, I feel that most of them are too complicated to write, or I just want to explain how to declare, define, implement, and use this stuff. I am dizzy and know how to use it, but I don't know why I want to use it, so I can't understand it very well.

Next, let's talk about my personal understanding, which may not be accurate. If there is something wrong with understanding, I hope the netizens can contact me and correct me and help me learn from each other ~

Delegate:

When I first saw the delegate, I directly thought of the interface to recall the interface.

When we want to do something, we need something. We want this thing to do something, but we don't know how to do it, so we define an interface, it is full of abstract methods used to tell others that we want to do these things. Then, you can throw the interface to others for specific implementation. We just need to call the interface.

I understand that the same is true of delegation. For example, if you want to build a house for yourself, you should draw the design drawings and then throw them to the secretary and say to him, "You can find someone, let them build the house for me."

Is it very similar to the interface?

But if you just want someone to help you do one thing, and that person can do many of his own things, it is unnecessary to define an interface only when you ask him to do it?

Delegate, which is a reference type and points to a method. Therefore, when defining a delegate, it looks more like defining a method than defining a class.

   PlayMedia playmedia;

Use the delegate keyword to create a delegate.

The event keyword tells the editor that this delegate can only define the class calls of the delegate, and can only be subscribed to or unsubscribed by other classes using the + = and-= operators, respectively, of course, the event keyword is not allowed here.

The delegate implementation method must have the same return type and signature as the delegate, but both non-static and static methods can be used.

Event:

An event is actually a delegate, but a delegate can be anything, but an event usually refers to some actions or some state changes, and the events in. net also have their own specifications.

. In net, the event processor usually returns the void type and obtains two parameters. The first parameter is the instance that defines the delegate class, and the second parameter is an EventArgs (the base class of event data).

 

  Main(=             ClockA a = =             Hour { ;   Minute { ;   Second { ;  TimeInfoEventArgs( hour,  minute, .Hour =.Minute =.Second =                                                          SecondChangeHandler(                          Thread.Sleep(                System.DateTime dt =                 (dt.Second !=                     (SecondChanged !=                         SecondChanged(,                     .second =         +=   TimeHasChanged( +=   TimeHasChanged(

 

 

 

 

  

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.