Shekhar_Pro Shekhar_Pro - 4 months ago 8x Question

Why is there a default instance of every form in VB.Net but not in C#?

I'm just curious to know that there is the (Name) property, which represents the name of the Form class. This property is used within the namespace to uniquely identify the class that the Form is an instance of and, in the case of Visual Basic, is used to access the default instance of the form.

Now where this Default Instance come from, why can't C# have a equivalent method to this.

Also for example to show a form in C# we do something like this:

// Only method
Form1 frm = new Form1();

But in VB.Net we have both ways to do it:

' First common method

' Second method
Dim frm As New Form1()

  1. My question comes from this first method. What is this
    , is it an instance of
    or the
    class itself? Now as I mentioned above the Form name is the Default instance in VB.Net. But we also know that
    is a class defined in
    so how can the names be same for both the Instance and class name?
    is a class then there is no (Static\Shared) method named Show().
    So where does this method come from?

  2. What difference they have in the generated IL?

  3. And finally why can't C# have an equivalent of this?


This was added back to the language in the version of VB.NET that came with VS2005. By popular demand, VB6 programmers had a hard time with seeing the difference between a type and a reference to an object of that type. Form1 vs frm in your snippet. There's history for that, VB didn't get classes until VB4 while forms go all the way back to VB1. This is otherwise quite crippling to the programmer's mind, understanding that difference is very important to get a shot at writing effective object oriented code. A big part of the reason that C# doesn't have this.

You can get this back in C# as well, albeit that it won't be quite so clean because C# doesn't allow adding properties and methods to the global namespace like VB.NET does. You can add a bit of glue to your form code, like this:

public partial class Form2 : Form {
    [ThreadStatic] private static Form2 instance;

    public Form2() {
        instance = this;

    public static Form2 Instance {
        get {
            if (instance == null) {
                instance = new Form2();
                instance.FormClosed += delegate { instance = null; };
            return instance;

You can now use Form2.Instance in your code, just like you could use Form2 in VB.NET. The code in the if statement of the property getter should be moved into its own private method to make it efficient, I left it this way for clarity.

Incidentally, the [ThreadStatic] attribute in that snippet is what has made many VB.NET programmers give up threading in utter despair. A problem when the abstraction is leaky. You are really better off not doing this at all.