Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Subscribing and unsubscribing from events.

Subscribing and unsubscribing from events.

Scheduled Pinned Locked Moved C#
performancequestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Steve_
    wrote on last edited by
    #1

    I have a quick question for anyone who knows the answer to this: Is there a difference (either performance or otherwise) between the following:

    myObject.SomeEvent += new EventHandler(HandlingMethod);

    and

    myObject.SomeEvent += HandlingMethod;

    Thanks Steve

    T A 2 Replies Last reply
    0
    • S Steve_

      I have a quick question for anyone who knows the answer to this: Is there a difference (either performance or otherwise) between the following:

      myObject.SomeEvent += new EventHandler(HandlingMethod);

      and

      myObject.SomeEvent += HandlingMethod;

      Thanks Steve

      T Offline
      T Offline
      Thomas Stockwell
      wrote on last edited by
      #2

      One is correct and the other isn't. You need to do the '+= new EventHandler' for non-anonymous methods. To unsubscribe from events you can use the -= .

      Regards, Thomas Stockwell Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. Visit my Blog

      1 Reply Last reply
      0
      • S Steve_

        I have a quick question for anyone who knows the answer to this: Is there a difference (either performance or otherwise) between the following:

        myObject.SomeEvent += new EventHandler(HandlingMethod);

        and

        myObject.SomeEvent += HandlingMethod;

        Thanks Steve

        A Offline
        A Offline
        Alan N
        wrote on last edited by
        #3

        The short answer is no, they are equivalent. A slightly longer answer is that the compiler recognises from the context that the second version requires the new EventHandler(...) and generates the same code for both. For a really detailed answer take a look at a copy of Jeff Richter's "CLR via C#". But I warn you, that may tell you more than you wanted to know! Alan.

        S 1 Reply Last reply
        0
        • A Alan N

          The short answer is no, they are equivalent. A slightly longer answer is that the compiler recognises from the context that the second version requires the new EventHandler(...) and generates the same code for both. For a really detailed answer take a look at a copy of Jeff Richter's "CLR via C#". But I warn you, that may tell you more than you wanted to know! Alan.

          S Offline
          S Offline
          Steve_
          wrote on last edited by
          #4

          Thanks. I guessed that was what would happen but wanted clarification.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups