6

What is the difference between

public delegate void SecondChangedHandler(Object clock, TimeInfoEventArgs ti);
public event SecondChangedHandler OnSecondChanged;

and

public event EventHandler<TimeInfoEventArgs> OnSecondChanged;

When should you use which and is there any best practice?

2 Answers 2

9

Before generics the first approach was the only option to declare an event handler delegate. Since generics were introduced in .NET 2.0, EventHandler<T:EventArgs> is the preferred way. Semantically there is no difference between them.

3

There is no difference indeed. Then EventHandler<T> (msdn) has just been added as a "shortcut" to avoid having to redeclare the delegate.

1
  • +1 - It is indeed to avoid having to redeclare the delegate. Feb 21, 2012 at 4:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.