My friend is primarily a VB developer and he says time and time again how much more simple it is to code events in VB than C#. My take on the issue is that it probably is easier but if there was not a reason for the added complexity, they probably would have made it just as simple in C#. Can anyone tell me if there is any added flexibility or any ability, in general, that can be done with C# events and not VB.Net events?
views:
319answers:
5I prefer VB to C# most of the time, but I'm pretty fluent in both.
Off the top of my head there are two places I know where VB.Net makes it easier. One is that you don't ever have to check for null
before raising an event. There might be some trade-off there, but I"m not aware of it.
The other is the addition of the Handles
keyword. You can declare a full method to handle event and wire it up to the event in one statement. This is a definite advantage for VB, because you could still do everything in long form without that keyword. It's just an extra little piece of syntactic sugar. The only way you can do that in C# is with a lambda expression/anonymous delegate.
The rest of the syntax is pretty much a wash: do you prefer "+=
" or "AddHandler
"?
With VB, can't you imply a signature, rather than stating a delegate type?
Public Event OnChange(ByVal Text As String)
I'm not sure it is a good thing, but...
Also - doesn't RaiseEvent
handle nulls (non-subscribed events) automatically? Trivial to do, of course.
The only thing that comes to mind for C# is the ability to subscribe to a void returning event (virtually all events) with an anonymous fuction. VB.Net 9.0 only supports Lambda Expressions which returns a value (this is fixed in VB 10.0).
VB has a bit of flexbility not present in C# with regard to events
- Support for Relaxed Delegates. This allows VB to use event handlers which only need a subset of the parameters in the event type (mostly used with empty parameter functions)
- The Handles clause makes it much easier to delete designer generate events as opposed to C# where you have to dig through the .Designer.cs file
- The RaiseEvent keyword makes the null event check problem non-existent in VB.Net
Your friend is probably used to creating event handlers by selecting the control and event from the two combo-boxes on top of the source code page. Where as in C# you manually code the += handler.
In C# you can have virtual events and override them in derived class. There is no such thing in VB.NET.