Why no constraint on EventHandler<TEventArgs>?

北城余情 提交于 2019-12-08 19:41:36

问题


I just figured out by accident (when something compiled that I didn't think would compile) that EventHandler is not constrained to the type System.EventArgs.

Here's the inline docs:

#region Assembly mscorlib.dll, v4.0.0.0
// C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\mscorlib.dll
#endregion

namespace System
{
    // Summary:
    //     Represents the method that will handle an event.
    //
    // Parameters:
    //   sender:
    //     The source of the event.
    //
    //   e:
    //     An System.EventArgs that contains the event data.
    //
    // Type parameters:
    //   TEventArgs:
    //     The type of the event data generated by the event.
    [Serializable]
    public delegate void EventHandler<TEventArgs>(object sender, TEventArgs e);
}

Is this a mismatch between docs and implementation?

I'm asking because I am curious. It's not a complaint at all.


回答1:


The type constraint was removed in .net 4.5.

Here is the .net 4.5 signature. http://msdn.microsoft.com/en-us/library/db0etb8x%28v=vs.110%29.aspx

[SerializableAttribute]
public delegate void EventHandler<TEventArgs>(
    Object sender,
    TEventArgs e
)

Here is the .net 4.0 signature. http://msdn.microsoft.com/en-us/library/db0etb8x%28v=vs.100%29.aspx

[SerializableAttribute]
public delegate void EventHandler<TEventArgs>(
    Object sender,
    TEventArgs e
)
where TEventArgs : EventArgs


来源:https://stackoverflow.com/questions/15736970/why-no-constraint-on-eventhandlerteventargs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!