Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Are the default objects supported by My keyword in VB.net is a kind of singleton? If not, what is the difference?

Again is this "default object" feature is something useful? If yes, then why it is not implemented in other languages like C#?

share|improve this question
add comment

2 Answers

My is a namespace not a singleton, but the "default objects supported by My keyword" are thread-local singletons (i.e. one instance per thread) as can be seen when using Reflector.

And the ThreadSafeObjectProvider<T> that provides this support is generated in each assembly compiled by VB.NET and is Friend so it is not available for external use.

Nevertheless its implementation is easy to replicate in your own ThreadSafeSinglton<T>. It is effectively a normal .NET singleton pattern except the backing field has the <ThreadStatic> attribute.

share|improve this answer
add comment

It is not a Singleton.

My is just a namespace containing static classes, methods and properties that point to other parts of the .NET framework, and provide some additional capabilities (e.g. CopyDirectory) Think of it as a set of shortcuts, a speed-dial of sorts.

Visual Basic provides language/compiler support with the My keyword. You can use the My classes in C# by adding a reference to Microsoft.VisualBasic.dll, but it is awkward, and not all classes are compatible with C# (they are VB specific). In C#, unless you want some of the VB capabilities (like "CopyDirectory"), it is more convenient to just reference the .NET framework classes directly, IMO.

Visual Basic has a somewhat different language philosophy than C# does. Originally, VB was to be the "applications" language, and C# was to be the "systems" language. Since then, the two languages have converged, and the feature sets of the two are almost indistinguishable, with a few notable differences (this being one, XML Literals being another).

I am quite certain that, if you asked Eric Lippert or one of the other members of the C# compiler team, they would tell you that implementing new features is a tug-of-war between available resources and feature importance, and that this particular feature just never hit threshold.

Navigate The .NET Framework And Your Projects With The My Namespace
http://msdn.microsoft.com/en-us/magazine/cc163680.aspx

My Namespace
http://msdn.microsoft.com/en-us/vbasic/ms789188

share|improve this answer
    
So, "why it is not implemented in other languages like C#?" –  Gulshan Dec 20 '10 at 16:54
    
@Gulshan: I added some additional detail about that. –  Robert Harvey Dec 20 '10 at 16:56
    
It was implemented in vb.net to make the transition from VB6 to VB.net easier, or at least more appealing to VB6 programmers. Something like it might make it's way to C#, though C# seems more attached to modularity and OOP –  Martijn Dec 20 '10 at 19:01
2  
Though the capabilities of the languages have converged, there are still differences in design philosophy and in typical user preferences. The "My" feature, like XML literals, fits well with the VB philosophy of making common tasks highly discoverable to opportunistic programmers. The more "general" C# philosophy eschews special treatment for specific tasks. And yes, of course there are budgetary concerns as well, as you note. –  Eric Lippert Dec 22 '10 at 16:02
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.