I try to stick as much as possible to the Framework Design Guidelines even when designing class libraries intended only for internal use.
To a certain degree, it makes my life easier as I use Code Analysis to automate checking for places I may not be using best-practices.
But here's a case where I feel very compeled to just ignore the recommendations.
Since the EventArgs constraint was removed from EventHandler in .NET 4, why would anyone want to use a class derived from EventArgs (as prescribed at https://msdn.microsoft.com/en-us/library/ms229011(v=vs.110).aspx) nowadays instead of using the desired data type directly and avoiding the cost of allocating another object and embeding the desired data in it?
For instance, having to write the following:
public class ExceptionEventArgs : EventArgs
{
private readonly Exception _exception;
public ExceptionEventArgs(Exception exception)
{
_exception = exception;
}
public Exception Exception
{
get
{
return _exception;
}
}
}
public event EventHandler<ExceptionEventArgs>;
is much more verbose as costs an extra allocation instead of:
public event EventHandler<Exception>;
I even created a generic EventArgs for using on another class a couple of weeks ago, but I feel silly to do so just for the sake of sticking to the guidelines.
public class EventArgs<T> : EventArgs
{
private readonly T _data;
public EventArgs(T data)
{
_data = data;
}
public T Data
{
get
{
return _data;
}
}
}
So, are there any cons for using
EventHandler<NotAnEventArgsDerivedClass>
for classes intended for internal* use only?
*By that I really mean, for use on a product comprised of several assemblies - all of which are intended for use exclusively by the product itself.