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.

Aaron Anodide
  • 16,906
  • 15
  • 62
  • 121
  • 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) – Will Apr 01 '13 at 03:45

1 Answers1

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
Will
  • 10,013
  • 9
  • 45
  • 77
  • given that the documentation didn't change in MSDN i wonder if we found a minor bug here – Aaron Anodide Apr 01 '13 at 03:48
  • 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. – Will Apr 01 '13 at 22:43
  • 1
    Side note: the [CA1009](https://msdn.microsoft.com/en-us/library/ms182133.aspx) warning didn't go away either, so using anything that doesn't derive from EventArgs will still trigger an FxCop warning. – Clément Jan 11 '14 at 21:16