为什么在 VB.Net 中每个表单都有一个默认实例,而在 C# 中却没有? [英] Why is there a default instance of every form in VB.Net but not in C#?

查看:30
本文介绍了为什么在 VB.Net 中每个表单都有一个默认实例,而在 C# 中却没有?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想知道有 (Name) 属性,它表示 Form 类的名称.此属性在命名空间中用于唯一标识 Form 是其实例的类,并且在 Visual Basic 的情况下,用于访问表单的默认实例.

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.

既然这个 Default Instance 是从哪里来的,为什么 C# 不能有一个与之等效的方法.

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

例如,为了在 C# 中显示表单,我们执行以下操作:

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

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

但在 VB.Net 中我们有两种方法:

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

' First common method
Form1.Show()

' Second method
Dim frm As New Form1()
frm.Show()

  1. 我的问题来自第一种方法.这个 Form1 是什么,它是 Form1 的实例还是 Form1 类本身?现在正如我上面提到的,表单名称是 VB.Net 中的默认实例.但是我们也知道 Form1 是在 Designer 中定义的一个类,那么 Instance 和类名怎么可能相同呢?如果 Form1 是一个类,则没有名为 Show() 的 (StaticShared) 方法.那么这个方法是从哪里来的呢?

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

它们在生成的IL中有什么区别?

What difference they have in the generated IL?

最后,为什么 C# 不能有这样的等价物?

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

推荐答案

这是在 VS2005 附带的 VB.NET 版本中添加回语言的.由于普遍的需求,VB6 程序员很难看出类型和对该类型对象的引用之间的区别.您的代码段中的 Form1 vs frm.有这方面的历史,VB 直到 VB4 才获得类,而表单一直回到 VB1.否则,这对程序员的头脑来说是相当严重的,理解差异对于编写有效的面向对象代码非常很重要.C# 没有这个的很大一部分原因.

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.

您也可以在 C# 中恢复它,尽管它不会那么干净,因为 C# 不允许像 VB.NET 那样向全局命名空间添加属性和方法.您可以在表单代码中添加一点胶水,如下所示:

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() {
        InitializeComponent();
        instance = this;
    }

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

您现在可以在代码中使用 Form2.Instance,就像在 VB.NET 中使用 Form2 一样.应该将属性 getter 的 if 语句中的代码移到其自己的私有方法中以使其高效,为了清楚起见,我将其保留为这种方式.

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.

顺便说一句,该片段中的 [ThreadStatic] 属性使许多 VB.NET 程序员完全绝望地放弃了线程.抽象泄漏时的问题.你真的最好不要这样做.

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.

这篇关于为什么在 VB.Net 中每个表单都有一个默认实例,而在 C# 中却没有?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆