After a bit of time away, I'm teaching part-time again. We were covering static members in VB and it ocurred to me (and the class) that static methods in VB exhibit different behaviour than in C#. I do a lot of my programming with C#, and I was explaining how Shared (the VB equivalent to the C# static keyword) methods are available through instances of the class instead of just the type itself. So, if I make a DeckOfCards object with a Shared (static) method named Sort(), the following is valid VB.Net:
dim objInstance as DeckOfCards = new DeckOfCards()
objInstance.Sort( args )
In C#, the only way to call the static Sort method is through the type:
DeckOfCards.Sort( args );
Four things strike me:
- This is a fairly trivial thing . . .
- . . . Except that, to be technically correct, it means you have to caveat your explanation of Static methods based on each language.
- This is possibly (although I'm not sure) why VB.Net uses the keyword Shared in place of “static“. Shared is not completely identical to the C# static.
- I like the c# way better; it's cleaner. For example, the next question a student will ask is “can I reference the current instance though the Shared member?“ The rationale for static/shared methods comes into question . . .
I can't think of a “good” reason for VB.Net exhibiting this behaviour, but I'm not on the VB.Net language team so who knows; it's not the first strange difference I've seen (see my post here for example) and it won't be my last. Anyway, I thought I should document it here for posterity.
Happy .Netting!