Subscribing and unsubscribing from events.
-
-
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
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
-
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
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.
-
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.