0

Possible Duplicate:
Is there an actual difference in the 2 different ways of attaching event handlers in C#?

I'm working on a .NET application and I ran into a small syntax dilemma. I noticed there were two ways of unsubscribing from an event and I'm curious. Is there any difference between the two, or are they equal? Is there any scenario, when they may act differently? Compiler doesn't seem to have problem with either of those.

object.myEvent += new EventHandler(callback_function);
object.myEvent -= new EventHandler(callback_function);

and

object.myEvent += new EventHandler(callback_function);
object.myEvent -= callback_function;

Thanks

Community
  • 1
  • 1

2 Answers2

0

Wrapping a delegate in the EventHandler is redundant. You can always drop it even though it is a part of the standard template.

Resharper gives you a warning about this

Servy
  • 202,030
  • 26
  • 332
  • 449
mfeingold
  • 7,094
  • 4
  • 37
  • 43
0

There isn't any difference. You could also have:

object.myEvent += callback_function;
object.myEvent -= callback_function;

The compiler does some nifty type inference which makes code readability a lot better!

Lews Therin
  • 10,907
  • 4
  • 48
  • 72