C# Event Programming – Should the ‘e’ Parameter Be Omitted in EventHandler?

cevent-programmingparameters

Whenever I am defining my own events, I use a pattern such as the following (which I believe is how the MSDN recommends doing it):

public class MyEventClass
{
    private bool _value; // Backing variable
    public bool Value
    {
        get { return _value; }
        set
        {
            if (value != _value) // Only raise event if value is changed/different
            {
                _value = value;
                OnValueChanged(EventArgs.Empty);
            }
        }
    }

    public event EventHandler ValueChanged; // Anything can subscribe to the event
    protected void OnValueChanged(EventArgs e) // Only this and children can invoke event
    {
        if (ValueChanged != null)
            ValueChanged(this, e);
    }
}

Thus, the client code just needs to subscribe to the event and it automagically works.

However, it feels cumbersome to pass the EventArgs e parameter, in particular with a class that uses EventHandler to raise its events, since the base EventArgs has no data to it, only EventArgs.Empty.

Would it be considered a faux pas to change it to this?

protected void OnValueChanged()
{
    if (ValueChanged != null)
        ValueChanged(this, EventArgs.Empty);
}

Best Answer

Is it wrong? No. Will it cause people to complain about your code? No.

Will it sidetrack some busy developer for a couple of minutes while he tries to find out what the parameters are and whether they will help solve his problem? Could be..

Will it make your class easier to use...?

Standards and patterns are useful because they are easily recognized and used (or ignored). Deviating from conventions should be done for a purpose, not just because you can. This applies whether it is bracket style, parameter names or argument list. Do it for a reason, not just because it compiles.