就像在Visual Studio中的默认C#的模板,我已经定义了一个Windows Form
作为一个静态对象,如下图所示:我是否需要处理静态对象内定义的非静态对象?
public static FormMain formMain;
static void Main()
{
formMain = new FormMain();
Application.Run(formMain);
formMain.Dispose();
}
正如你所看到的,我分配(使用new
)的内存空间这个静态表单在调用它之前,在表单关闭后释放内存(使用Dispose
)。
然而,这种静态的形式中,我定义了几个非静态物体(比如标签)的,如下图所示:
public FormMain()
{
// some code here
Label myLabel1 = new Label();
Label myLabel2 = new Label();
Label myLabel3 = new Label();
// some code here
}
现在,我有两个问题:
我是否还必须
Dispose
这些非静态对象,或者它们是否在调用formMain.Dispose();
行后立即释放(释放内存)?如果我需要处理这些非静态对象,在哪个阶段的程序我应该更喜欢使用
Dispose
(比如,在FormClosed
或FormClosing
事件)?
注意:我尽量不使用表单设计工具在Visual Studio,但更喜欢通过代码行表格线。
Application.Run已经在示例中处理了这个表单:“Form类的Dispose方法将在返回[Application.Run]方法之前被调用。” – user2864740
我不是在询问窗体,而是在窗体中定义的非静态对象('myLabel1','myLabel2'等)。 – ssd
'FormMain'应该处理它创建的成员变量。 “FormMain”是否存储在一个静态变量中无关紧要。 –