Overload the += event operator

SwDevMan81 picture SwDevMan81 · Jul 15, 2009 · Viewed 7.8k times · Source

Is there a way to overload the event += and -= operators in C#? What I want to do is take an event listener and register it to different events. So something like this:

SomeEvent += new Event(EventMethod);

Then instead of attaching to SomeEvent, it actually attaches to different events:

DifferentEvent += (the listener above);
AnotherDiffEvent += (the listener above);

Thanks

Answer

Dykam picture Dykam · Jul 15, 2009

It's not really overloading, but here is how you do it:

public event MyDelegate SomeEvent
{
    add
    {
        DifferentEvent += value;
        AnotherDiffEvent += value;
    }
    remove
    {
        DifferentEvent -= value;
        AnotherDiffEvent-= value;
    }
}

More information on this on switchonthecode.com