Regarding Events and Delegates in C# -


what side-effects of considering events instances of delegates type?

jon skeet says, "events aren't delegate instances.", here.. not have asked if had read anywhere else.

i had always, past 2 months, visualized events special type of delegate event keyword helping in preventing nullification of delegate being invoked via event.

could please elaborate on how visualize whole concept new c# , event based programming?

edit 1:

i understood concepts of delegates , consequently events turned out simple concept. go ahead , add example conjured me during battle these constructs. have added lot of comments better understanding. meant new guys me:

the library dll:

namespace dosomethinglibrary {     /*      *this public delegate declared @ base namespace level global presence.      *the question why need have delegate here?      *the answer is: not want implement logging logic. why? well, consumers many      *and equally demanding. need different types of logging. need html logging,       *some need xml logging custom log analyzer, need plain text logging etc...      *this hell me. how going support demands. cannot. thus, ask them       *implement logging on side. providing interface(literal sense) in guise of delegate.      *a delegate hook.      *this hook needed consumers hook custom loggers library.      */     public delegate void logger(string firstparam, string secondparam);      public class printingmaniac     {         public logger printingmaniacconsumerloggerhook;         public void startprintinglikeamaniac()         {             (int iterator = 0; iterator <= 3; iterator++)             {                 /*this loop emulator using emulate huge processing or huge job.                  *let imagine library heavy data crunching or                   *extremely complex data access job etc..                  */                 console.writeline("actual work - " + iterator.tostring());                 /*after each step library tries log. note library                  *has no logger implemented. instead, library has judiciously delegated                  *the logging responsibilty consumer of library.                  */                 printingmaniacconsumerloggerhook("actual work", "step " + iterator.tostring());             }         }     } } 

the consumer executable:

/*  * let assume have purchased dosomethinglibrary dll vendor.  * have add dll reference executable's project in visual studio.  * have use dosomethinglibrary namespace access logic in dll.  */ using dosomethinglibrary;  namespace understandingdelegates {     class program     {         static void main(string[] args)         {             /*              * creating object of lone class printingmaniac in dosomethinglibrary              */             printingmaniac newmaniac = new printingmaniac();              /*              * hooking custom logger dosomethinglibrary dll.              * best of both worlds. have well-tested , efficient library working me              * , have best logging avaliable.              * dosomethinglibrary dll has no knowledge of logging executable going use.              * executable has satisfy requirements of delegate signature of dosomethinglibrary dll.              */             newmaniac.printingmaniacconsumerloggerhook += new logger(clientscustomizedloggertwo);              newmaniac.startprintinglikeamaniac();             console.readline();         }          public static void clientscustomizedloggerone(string firstparam, string secondparam)         {             /*              *this logger has '=' used decorator              *in real scenarios logger may complex.              *let assume html logger              */             console.writeline("=============================");             console.writeline("delegated logging in consumer code " + firstparam + " - " + secondparam);             console.writeline("=============================");         }          public static void clientscustomizedloggertwo(string firstparam, string secondparam)         {             /*              *this logger has '-' used decorator              *let assume xml logger              */             console.writeline("------------------------------");             console.writeline("delegated logging in consumer code " + firstparam + " - " + secondparam);             console.writeline("------------------------------");         }     } } 

edit 2:

i have written article in codeproject explain whole concept of delegates.

how visualize whole concept

nobody ever has trouble visualizing properties in c#. property accessor field, prevents other code directly manipulating field value. such code forced call get , set accessors access field. can put arbitrary code in accessor methods, throwing exception when not happy value passed setter example, common. underlying storage property doesn't have field either, can expose field or property of class object example. etcetera.

a way visualize event compare property. exact same intent, prevents other code directly manipulating delegate object. have go through add , remove accessors. calls these methods generated syntax sugar in client code, += operator calls add(), -= calls remove(). comparable syntax sugar accessing properties, don't explicitly write call or set method either.

what confusing events , makes them seem different properties event accessor methods optional. if don't write them c# compiler auto-generate them you. including backing store, delegate object. properties can have auto-generated accessors , backing store, automatic properties do. syntax different, still have declare them.

using auto-generated accessors very common. code enough, provides guarantee arbitrary code cannot remove event subscriptions of other code. not many reasons write own. 1 cutting down on size of class object if support lot of events. instead of having delegate object each individual event, can store them in eventhandlerlist instead. common in .net framework code example. indirection taken advantage of in wpf's attached events , winrt's eventing model isn't based on delegates.


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -