Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overload the += event operator

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

like image 261
SwDevMan81 Avatar asked Jul 15 '09 15:07

SwDevMan81


People also ask

How do you overload an operator?

Overloaded operators are just functions (but of a special type) with a special keyword operator followed by the symbol of the operator to be overloaded.

Can we overload << operator in C++?

We can overload the '>>' and '<<' operators to take input in a linked list and print the element in the linked list in C++. It has the ability to provide the operators with a special meaning for a data type, this ability is known as Operator Overloading.

What do you mean by operator overloading?

Polymorphism: Polymorphism (or operator overloading) is a manner in which OO systems allow the same operator name or symbol to be used for multiple operations. That is, it allows the operator symbol or name to be bound to more than one implementation of the operator. A simple example of this is the “+” sign.

What is operator overloading in C++?

Operator overloading is a compile-time polymorphism in which the operator is overloaded to provide the special meaning to the user-defined data type. Operator overloading is used to overload or redefines most of the operators available in C++. It is used to perform the operation on the user-defined data type.


1 Answers

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

like image 66
Dykam Avatar answered Oct 18 '22 12:10

Dykam