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
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With