I've been playing around with anonymous functions and delegates. It's left me wondering what advantages the traditional ways of declaring events have.
Consider this inside of a class.
public delegate void MyHandler(string input);
public MyHandler OnSomethingHappens;
// ...then, somewhere else
OnSomethingHappens("The thing that happened...");
From outside the method...
MyClass.OnSomethingHappens += (input) => { Console.WriteLine("Do something"; };
MyClass.OnSomethingHappens += (input) => { Console.WriteLine("Do something else"; };
How is this any different than a traditional event handling situation, where I code up a SomethingHappensEventArgs
class, wire it to an actual event, etc.?
With the code above, I am able to assign code to execute when the class says something has happened...which is essentially what an event does.
Why would I go to all the trouble of creating a "true" event? Dare I say it...Intellisense icons? :-)