Custom EventHandler vs. EventHandler

前端 未结 2 1184
萌比男神i
萌比男神i 2021-01-30 12:48

Recently I\'ve been wondering if there is any significant difference between this code:

public event EventHandler SomeEvent;

相关标签:
2条回答
  • 2021-01-30 13:00

    You're right; they do the same thing. Thus, you should probably prefer the former over the latter because it's clearer and requires less typing.

    The reason that lots of the .NET Framework classes have their own custom event handler delegates is because they were written before generics (which allowed the shorthand syntax) were introduced in version 2.0. For example, almost all of the WinForms libraries were written before generics, and back in those days, the latter form was the only way of doing things.

    0 讨论(0)
  • 2021-01-30 13:05

    The second way gives more flexibility and type safety. There are less methods with corresponding signature => less place for a mistake. Custom delegate allows to specify exactly parameters you need (or specify no one) - no sender+args cargo cult.

    0 讨论(0)
提交回复
热议问题