Re: [aus-dotnet] C# Newbie question relating to Delegates/Events


    [Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
    • From: Jano Petras
    • Subject: Re: [aus-dotnet] C# Newbie question relating to Delegates/Events
    • Date: Thu, 27 Oct 2005 18:59:32 +1000

    Delegate is like a pointer to the function (type safe pointer that
    is). It means it can hold an address of a function in memory of given
    type and this value is provided at run time. At one instance, it can
    call myfunction1() and at another myfunction2() based on which address
    you provided to it.
    
    You can call the function in question through the delegate.
    
    For example, you have two functions to do some related tasks. Based on
    data, you may want to invoke one or another but this is known only at
    runtime.
    
    You can create a generic function that will accept a delegate variable
    and invoke a function through the delegage (sort of a "late binding
    behaviour" in lame terms). The actual function called depends on
    passed value and is not known at compile time.
    
    The event is a simple use-case of the delegates. Think about an event
    as of an ArrayList of delegates. Different pieces of code can
    "subscribe" to the event by supplying their addresses which will be
    stored in this ArrayList for the event.
    
    Once the event publisher decides to raise the event, it will
    internally go though this ArrayList of delegates and invoke each one -
    so all subscribers will be given a chance to respond to the event in
    the function whose address they provided when subscribing.
    
    It is like this mailing list: when new mail arrives into it, all
    subscribers will receive a copy. Analogy is that all emails are like
    addresses, the delegate will be similar to the database field holding
    the email address, and the event is arrival of the new mail to the
    list. Therefor, the event is raise, all delegates are invoked - i.e.
    the email is send to all email addresses stored in delegates.
    
    Hopefully it makes sense - if not, at least I know I tried my best
    
    -- Jano
    
    On 27/10/05, Simon Kuldin <sk@xxxxxxxxxxxxxxxxxx> wrote:
    >
    >
    > Hi there,
    >
    >
    >
    > As I have mentioned a few weeks ago, I am in the process of reading through
    > a Visual C# book to try and start grasping the concepts of C# syntax and
    > functionality.
    >
    >
    >
    > I have just completed a chapter on Delegates and Events, and thought I had a
    > ok-ish understanding of them, but a fellow programmer (who has tried to
    > expand my understanding), said that the book I am reading is not good enough
    > at helping to visual the whole concept.
    >
    >
    >
    > Is there anyone out there who may want to try and give their laymans terms
    > on the way that Delegates and Events are useful and why?  I mean the basic
    > concept of Events I understand, but for example, my co-worker tried to
    > explain how Events themselves are also Delegates.
    >
    >
    >
    > I hope this makes sense ;)  Thanking anyone in advance for any help that
    > they can provide.
    >
    >
    >
    > ________________________________________________________________________
    >
    > Simon Kuldin
    >
    > Senior Technical Consultant
    > Information Outlook
    > Microsoft Certified Business Solutions Partner
    > 4/566 St Kilda Rd Melbourne 3004
    > Ph: 9526 8478 - Fax: 9521 7111
    > e:sk@xxxxxxxxxxxxxxxxxx w:http://www.infooutlook.com.au
    > ________________________________________________________________________
    > This e-mail and any attachments are confidential and intended solely or the
    > use of the recipient to whom it
    > is addressed. If this has been received in error, please contact the sender
    > and destroy immediately.
    >
    > This e-mail remains the property of the sender.
    >
    > This e-mail has been scanned for viruses using Norton AntiVirus Corporate
    > Edition.
    > ________________________________________________________________________
    >
    >
    >
    >
    _________________
    You are a part of the Ausralian "dotnet" mailing list. To unsubscribe send "unsubscribe dotnet" or to re-subscribe send "subscribe dotnet" in the body of the email to: imailsrv@xxxxxxxxxxx 
    List Managed by www.stanski.com and Proudly Sponsored by www.ico.com.au
    
    
    



    (Click here for more information on the aus-dotnet mailling list)