event Action<> vs event EventHandler<>

后端 未结 7 2201
野的像风
野的像风 2020-11-28 02:05

Is there any different between declaring event Action<> and event EventHandler<>.

Assuming it doesn\'t matter what object actua

相关标签:
7条回答
  • 2020-11-28 02:48

    Looking at Standard .NET event patterns we find

    The standard signature for a .NET event delegate is:

    void OnEventRaised(object sender, EventArgs args);

    [...]

    The argument list contains two arguments: the sender, and the event arguments. The compile time type of sender is System.Object, even though you likely know a more derived type that would always be correct. By convention, use object.

    Below on same page we find an example of the typical event definition which is something like

    public event EventHandler<EventArgs> EventName;
    

    Had we defined

    class MyClass
    {
      public event Action<MyClass, EventArgs> EventName;
    }
    

    the handler could have been

    void OnEventRaised(MyClass sender, EventArgs args);
    

    where sender has the correct (more derived) type.

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