Why no constraint on EventHandler<TEventArgs>?
Asked Answered
F

1

6

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.

Fino answered 1/4, 2013 at 2:41 Comment(1)
I wonder what happens if you build with visual studio 2012(.net 4.5) and try to run it on a machine without .net 4.5. (Not on a machine with vs 2012 so I can't check right now)Pilsen
P
5

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
Pilsen answered 1/4, 2013 at 3:37 Comment(3)
given that the documentation didn't change in MSDN i wonder if we found a minor bug hereFino
I imagine this change was intentional. They probably just forgot to update the msdn page's remark. I am not sure where you report that kind of thing.Pilsen
Side note: the CA1009 warning didn't go away either, so using anything that doesn't derive from EventArgs will still trigger an FxCop warning.Daunt

© 2022 - 2024 — McMap. All rights reserved.