C # delegate-based events

Source: Internet
Author: User

Events are based on the attributes of the multicast delegate.

  A multicast delegate is actually a set of type-safe function Pointer managers that are executed in order to jump to all function pointers in the array.

    classProgram { Public classCarinfoeventargs:eventargs { Public stringCar {Get;Set; }  PublicCarinfoeventargs (stringcar) {                 This. Car =car; }        }         Public classCardealer { Public EventEventhandler<carinfoeventargs> Newcarinfo;//using a system-defined generic delegate             Public voidNewcarccoming (stringcar) {Console.WriteLine ("Cardealer, new car {0} has come.", car); EventHandler<CarInfoEventArgs> Newcarinfo =Newcarinfo; if(Newcarinfo! =NULL) Newcarinfo ( This,NewCarinfoeventargs (car)); }        }         Public classConsumer {Private stringname;  PublicConsumer (stringname) {                 This. Name =name; }             Public voidNewcarishere (Objectsender, Carinfoeventargs e) {Console.WriteLine ("{0}, car {1} is new", name, E.car); }        }        Static voidMain (string[] args) {            vardealer =NewCardealer (); varPersonA =NewConsumer ("PersonA"); Dealer. Newcarinfo+=Persona.newcarishere; Dealer. Newcarccoming ("Ferrari"); varPERSONB =NewConsumer ("personb"); Dealer. Newcarinfo+=Personb.newcarishere; Dealer. Newcarccoming ("BMW");        Console.ReadLine (); }    }

Based on this example, we use the concept of "multicast delegation" to rewrite:

    classProgram { Public classCardealer { Publicaction<string> newcarinfo;//using a system-defined generic delegate             Public voidNewcarcoming (stringcar) {Console.WriteLine ("Cardealer, new car {0} has come.", car); if(Newcarinfo! =NULL) Newcarinfo (car); }        }         Public classConsumer {Private stringname;  PublicConsumer (stringname) {                 This. Name =name; }             Public voidNewcarishere (stringcar) {Console.WriteLine ("{0}, car {1} is new", name, car); }        }        Static voidMain (string[] args) {            vardealer =NewCardealer (); varPersonA =NewConsumer ("PersonA"); Dealer. Newcarinfo+=Persona.newcarishere; Dealer. Newcarcoming ("Ferrari"); varPERSONB =NewConsumer ("personb"); Dealer. Newcarinfo+=Personb.newcarishere; Dealer. Newcarcoming ("BMW");        Console.ReadLine (); }    }

I just want to know what are the characteristics of events on the basis of multicast delegation? From the above two examples, it does not seem to be visible.

Keep looking at the next example:

    classProgram { Public classCardealer { Public Eventaction<string> newcarinfo;//using a system-defined generic delegate             Public voidNewcarcoming (stringcar) {Console.WriteLine ("Cardealer, new car {0} has come.", car); if(Newcarinfo! =NULL) Newcarinfo (car); }        }         Public classConsumer {Private stringname;  PublicConsumer (stringname) {                 This. Name =name; }             Public voidNewcarishere (stringcar) {Console.WriteLine ("{0}, car {1} is new", name, car); }        }        Static voidMain (string[] args) {            vardealer =NewCardealer (); varPersonA =NewConsumer ("PersonA"); Dealer. Newcarinfo+=Persona.newcarishere; Dealer. Newcarinfo ("Ferrari"); varPERSONB =NewConsumer ("personb"); Dealer. Newcarinfo+=Personb.newcarishere; Dealer. Newcarinfo ("BMW");        Console.ReadLine (); }    }

1. We add the "event" keyword before the commission;

2. We invoke the delegate instance directly outside;

This example is compiled, so that "event" cannot be accessed directly from outside. Take out the "event" keyword for the instance and you can compile it.

 

    classProgram { Public classCardealer { Public Eventaction<string> newcarinfo;//using a system-defined generic delegate             Public voidNewcarcoming (stringcar) {Console.WriteLine ("Cardealer, new car {0} has come.", car); if(Newcarinfo! =NULL) Newcarinfo (car); }        }         Public classConsumer {Private stringname;  PublicConsumer (stringname) {                 This. Name =name; }             Public voidNewcarishere (stringcar) {Console.WriteLine ("{0}, car {1} is new", name, car); }        }        Static voidMain (string[] args) {            vardealer =NewCardealer (); varPersonA =NewConsumer ("PersonA"); Dealer. Newcarinfo+=Persona.newcarishere; Dealer. Newcarcoming ("Ferrari"); varPERSONB =NewConsumer ("personb"); Dealer. Newcarinfo+=Personb.newcarishere; Dealer. Newcarcoming ("BMW");        Console.ReadLine (); }    }

Will dealer. Newcarinfo ("BMW");

Changed to dealer. Newcarcoming ("BMW");

You can compile it by stating that the delegate instance can call the delegate method directly, and the event needs to be further encapsulated, so the event is the encapsulation of privatizing the delegate method .

The EXE is then checked by the anti-compiler:

As you can see, the addition of Add_newcarinfo, Remove_newcarinfo two methods, further proves our definition of the event: the compiler generated two public methods to resolve the issue of the external inability to increase or decrease the delegation method after the privatization of the delegate .

C # delegate-based events

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.