c# - 如果它不是静态方法,为什么我可以调用 Form.Close()

标签 c# vb.net winforms

有人能给我解释一下吗?

在 Visual Studio 2010 中,创建一个 VB.net Windows 窗体应用程序。添加 2 个表单:Form1 和 Form2。在 Form1 Load 事件中键入 Form2.Close()。现在,如果我们查看方法定义,Close() 不是静态(共享)方法。那么这怎么可能在运行时编译或工作。

此外,在 C# 和 Form2.Close() 中做同样的事情;不编译。

这是怎么回事?为什么这在 VB.net 中可行,执行该行代码时实际发生了什么?

最佳答案

您发现了一个名为“默认实例”的 VB.NET 风格。

编译器实际上发出了这个:

My.Forms.Form2.Close();

有一个不错的writeup of that feature here :

The default instance is an object of that type that the VB application framework creates and manages for you.

...

If you use the default instance then you don’t need to invoke a constructor explicitly. You simply access the default instance directly via the My.Forms object

关于c# - 如果它不是静态方法,为什么我可以调用 Form.Close(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8930344/

相关文章:

vb.net - Vb.Net 中的事件处理程序

屏幕调整大小时,CSS 百分比宽度无法正常工作

c# - .NET Windows 服务 - 架构决策

c# - 从 SqlDataReader 获取结果集的数量

c# - Selenium c# 接受确认框

c# - C# RegEx 匹配表达式中的可选元素

winforms - 使用多个面板设计 Windows.Form -> 如何隐藏一个面板(如 PS 层)

asp.net - 如何在frm(winForms)中创建PasswordHash asp身份o生成PasswordHash

c# - .ToArray() 的慢速 LINQ 查询

c# - 在创建关闭自身的匿名函数时使用未分配的局部变量