3 ms·
I hugely disagree with you thinking that event is useless in C#, rather I think it should have had been implemented as built-ins in every major programming lang
by stevefan1999 7y ago
I hugely disagree with you thinking that event is useless in C#, rather I think it should have had been implemented as built-ins in every major programming language in the style of what C# did.
Not only that it makes every possible event explicit and stand-out on its own, which is good for inline optimization and documentation (think about the catastrophic event handling in JS world), it also provides a standard, much more intuitive syntax using formal function delegate declaration (think type-safe function pointers), vastly different than what we do in JVM.
Before having lambdas in Java, we need to add an EventListener as a variable and adding an extra interface, so event handlers are insidious to write, that you have to write a new class, implement the specific interface, write some shim properties to store externally-living variables explicitly, and finally "new" that class as an instance, and add it to a specific event listener, which is not only verbose, and also costly, in terms of memory use (it has to be backed by vtables rather than simple functions) and time taken to implement it.
Well after the long-awaited introduction, Java finally have limited lambda support that just generates a class and it have some odd issues with, for example, enforced effectively final variable reference [0], but in C#, you have delegates and events almost from day 1 -- and it handles all that event mess nice and clean where nobody can still beat that simplicity and elegancy even till today.
[0]: https://stackoverflow.com/questions/34865383/variable-used-in-lambda-expression-should-be-final-or-effectively-final https://stackoverflow.com/questions/34865383/variable-used-i...
- MaxBarraclough 7y ago> it makes every possible event explicit and stand-out I agree that's nice to have - essentially announcing events as special in the type system. > good for inline optimization Any optimisation here should be possible with an ordinary implementation of the observer pattern, no? > it also provides a standard, much more intuitive syntax using formal function delegate declaration I'm not convinced that it does. Without events, we can still write: var h = () => { doStuff(); doOtherStuff(); }; subject.registerObserver(h); > Before having lambdas in Java, we need to add an EventListener as a variable and adding an extra interface I suspect we're both right, then: events were introduced for a good reason, but now that C# has lambdas and such, they don't seem to add much.
- jayd16 7y agoI'd much rather use events than that snippet.