Event Operator Overload + =

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

SomeEvent += new Event(EventMethod); 

Then, instead of binding to SomeEvent, it is actually bound to various events:

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

thanks

+6
c # events overloading operator-overloading event-listener
source share
4 answers

This is not overload, but here is how you do it:

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

Additional information on this at switchonthecode.com

+21
source share

You can do this in C # using specialized event accessors .

 public EventHandler DiffEvent; public EventHandler AnotherDiffEvent; public event EventHandler SomeEvent { add { DiffEvent += value; AnotherDiffEvent += value; } remove { DiffEvent -= value; AnotherDiffEvent -= value; } } 

This means that you can simply call SomeEvent += new EventHandler(Foo) or SomeEvent -= new EventHandler(Foo) , and the corresponding event handlers will be added / removed automatically.

+6
source share

You can combine delegates using the + and - operators

See How to Combine Delegates (Multicast Delegates) (C # Programming Guide)

0
source share

If you just want to get away from some typing, you can do it

 SomeEvent += MyMethod; 
-4
source share

All Articles