Comparison of delegation and event in C #

Source: Internet
Author: User

I. Example of delegated implementation:

// Define a delegate Type public delegate void DoSthDelegate1 ();////// Apple mp3 player ///Class iPod {// define a delegate variable in the iPod class public DoSthDelegate1 userDo; public void PlayMusic () {Console. writeLine ("start playing music ..... ^ ........ "); if (userDo! = Null) {// The userDo () action is triggered when the music is played at the beginning. Call the delegate variable. The specific value of the delegate variable is userDo ();} Console in the main program. writeLine ("start playing music ..... #########################......... ");} public void Stop () {Console. writeLine ("stop playing. "); // userDo ();} public void Next () {Console. writeLine ("play the next... ");}}
Main Program:

Static void Main (string [] args) {# iPod pod = new iPod (); pod. userDo = BlinkBackground implemented by region through delegation; // call the method for music to start playing. Pod. PlayMusic (); // You can overwrite the methods that you have already bound. // Pod. userDo = ShowLrc; // triggers userDo when no music is played. Here, the event // pod is introduced because it is impersonated. userDo (); # endregion static void ShowLrc () {Console. writeLine ("Today is a good day ,. today is a good day, today is a good day, today is a good day ,......... ");} static void BlinkBackground () {Console. writeLine ("Flashing ..... ");}}


II. Implementation through events

// Define a delegate. Public delegate void DoSthDelegate2 (); class iPod2 {// defines an event. //When defining an event, you must use a delegate. If you do not have a delegate, you cannot define the event.Public event DoSthDelegate2 MusicPlaying; public void PlayMusic () {Console. writeLine ("start playing music ..... ^ ........ "); if (MusicPlaying! = Null) {// This action is still triggered when the music starts to play. Call the event variable MusicPlaying ();} Console. writeLine ("start playing music ..... #########################......... ");} public void Stop () {Console. writeLine ("stop playing. ");} public void Next () {Console. writeLine ("play the next... ");}}
Main Program:

Static void Main (string [] args) {# effect achieved by region through the event iPod2 pod2 = new iPod2 (); // The event cannot be assigned directly with =, this avoids overwrite all the preceding content through =. Pod2.MusicPlaying + = new DoSthDelegate2 (BlinkBackground); pod2.PlayMusic, this event is triggered by an external impersonate event. It is impossible to call this directly // pod2.MusicPlaying (); pod2.MusicPlaying + = new prepare (pod2_MusicPlaying); // Add the event pod2.MusicPlaying-= pod2_MusicPlaying; // Delete the event # endregion Console. readKey ();} static void pod2_MusicPlaying () {throw new NotImplementedException ();} static void ShowLrc () {Console. writeLine ("Today is a good day ,. today is a good day, today is a good day, today is a good day ,......... ");} static void BlinkBackground () {Console. writeLine ("Flashing ..... ");}}

Summary:

1. "delegate" is a data type that can be stored as a method.

2. "Event" is an object, which can be understood as the encapsulation of delegate variables. The event is internally implemented by Delegate.

3. The event is compiled into an add_method () and remove_method () methods and a private delegate variable.

4. The defined event variables cannot be called outside the defined class, preventing the triggering of external impersonating events.

5. The role of delegation is actually used for placeholder calls. A delegate variable is used to replace the call of a method. assign a value to the delegate variable before the actual call. Otherwise, the value is null.

6. The function of an event is the same as that of a delegate, but it has more functional limitations than the delegate variable.



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.