What is the difference between event and multicast delegate

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • AliRezaGoogle

    What is the difference between event and multicast delegate

    Hi,
    I have a conceptual question on Events and Multicast Delegates. Let me
    explain:

    As we know an event is a multicast delegate. What we declare as an
    event is inherently a multicast delegate. I really do not undrestand
    what additional features the "event" keyword adds to multicast
    delegate. The only thing that I see as an additional feature is a
    "Thunder Icon" near event name in VS IDE when intellisense works;).

    Can anybody clarify this matter for me?
  • Cowboy \(Gregory A. Beamer\)

    #2
    Re: What is the difference between event and multicast delegate

    I believe event is derived from multicast delegate and not he other way
    around. If I understand it correct, event is a C# keyword that shortcuts the
    creation of a mutlicast delegate (or perhaps it would be better to say "a
    particular type of multicast delegate"). It is not a superclass for
    multicast delegate.

    The thunder icon is a feature of the IDE and does not affect the underlying
    classes. It is designed to make it easier for you to find your events.

    --
    Gregory A. Beamer
    MVP, MCP: +I, SE, SD, DBA

    Subscribe to my blog


    or just read it:


    *************** *************** **************
    | Think outside the box! |
    *************** *************** **************
    "AliRezaGoo gle" <asemoonya@yaho o.comwrote in message
    news:96ad8fac-3a7e-4d84-b195-43f4cf3ba59e@l4 2g2000hsc.googl egroups.com...
    Hi,
    I have a conceptual question on Events and Multicast Delegates. Let me
    explain:
    >
    As we know an event is a multicast delegate. What we declare as an
    event is inherently a multicast delegate. I really do not undrestand
    what additional features the "event" keyword adds to multicast
    delegate. The only thing that I see as an additional feature is a
    "Thunder Icon" near event name in VS IDE when intellisense works;).
    >
    Can anybody clarify this matter for me?

    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: What is the difference between event and multicast delegate

      AliRezaGoogle <asemoonya@yaho o.comwrote:
      I have a conceptual question on Events and Multicast Delegates. Let me
      explain:
      >
      As we know an event is a multicast delegate.
      No it's not. It's a pair of methods - add and remove, or subscribe and
      unsubscribe.

      See http://pobox.com/~skeet/csharp/events.html for a great deal more
      info on this.

      --
      Jon Skeet - <skeet@pobox.co m>
      Web site: http://www.pobox.com/~skeet
      Blog: http://www.msmvps.com/jon.skeet
      C# in Depth: http://csharpindepth.com

      Comment

      • Marc Gravell

        #4
        Re: What is the difference between event and multicast delegate

        As we know an event is a multicast delegate.

        No; an event is defined as a set of accessor methods that encapsulate
        a delegate in the same way as a property encapsulates a regular field.
        Where a property defined "get" and "set", an event defines "add" and
        "remove". That is all an event is. This provides the same abstraction
        as properties, and formalizes the contract: you can subscribe and
        unsubscribe, but you don't have direct control of the delegate.

        In particular, this prevents 2 unwanted scenarios:

        a: obj.SomeEvent = SomeHandler;
        (damn, what happened to the other subscribers?)

        b: obj.SomeEvent(. ..)
        (only "obj" should be invoking the event - not external callers)

        It also allows for abstraction in terms of how the delegate is stored.
        A good example of this is windows forms; they have lots of events,
        which most of the time aren't subscribed. Having a delegate field per
        event would be expensive, so instead an EventHandlerLis t is used to
        store sparse subscriptions efficiently. You could also (for example)
        do other processing when adding/removing subscribers: check access,
        facade to another property, lazy loading, etc.

        Marc

        Comment

        • AliRezaGoogle

          #5
          Re: What is the difference between event and multicast delegate

          Thanks a lot John. I followed your link and the article clarified
          everything for me. Many thanks man.

          Jon Skeet [ C# MVP ] wrote:
          AliRezaGoogle <asemoonya@yaho o.comwrote:
          I have a conceptual question on Events and Multicast Delegates. Let me
          explain:

          As we know an event is a multicast delegate.
          >
          No it's not. It's a pair of methods - add and remove, or subscribe and
          unsubscribe.
          >
          See http://pobox.com/~skeet/csharp/events.html for a great deal more
          info on this.
          >
          --
          Jon Skeet - <skeet@pobox.co m>
          Web site: http://www.pobox.com/~skeet
          Blog: http://www.msmvps.com/jon.skeet
          C# in Depth: http://csharpindepth.com

          Comment

          • AliRezaGoogle

            #6
            Re: What is the difference between event and multicast delegate


            Thanks a lot Marc. You are right in this point. Many thanks for your
            help.

            Marc Gravell wrote:
            As we know an event is a multicast delegate.
            >
            No; an event is defined as a set of accessor methods that encapsulate
            a delegate in the same way as a property encapsulates a regular field.
            Where a property defined "get" and "set", an event defines "add" and
            "remove". That is all an event is. This provides the same abstraction
            as properties, and formalizes the contract: you can subscribe and
            unsubscribe, but you don't have direct control of the delegate.
            >
            In particular, this prevents 2 unwanted scenarios:
            >
            a: obj.SomeEvent = SomeHandler;
            (damn, what happened to the other subscribers?)
            >
            b: obj.SomeEvent(. ..)
            (only "obj" should be invoking the event - not external callers)
            >
            It also allows for abstraction in terms of how the delegate is stored.
            A good example of this is windows forms; they have lots of events,
            which most of the time aren't subscribed. Having a delegate field per
            event would be expensive, so instead an EventHandlerLis t is used to
            store sparse subscriptions efficiently. You could also (for example)
            do other processing when adding/removing subscribers: check access,
            facade to another property, lazy loading, etc.
            >
            Marc

            Comment

            Working...