我知道C#是一种自动垃圾收集语言,但是如果我处理表单会不会有所不同?例如:

SettingsDialog dialog;
private void settingsToolStripMenuItem_Click(object sender, EventArgs e)
{
    dialog = new SettingsDialog();
    dialog.ShowDialog();
    dialog.FormClosed += new FormClosedEventHandler(dialog_FormClosed);
}
void dialog_FormClosed(object sender, FormClosedEventArgs e)
{
    dialog.Dispose();
}


或者我应该将其保留为:

private void settingsToolStripMenuItem_Click(object sender, EventArgs e)
{
    SettingsDialog dialog = new SettingsDialog();
    dialog.ShowDialog();
}


另外,如果SettingsDialog占用大量内存怎么办?这会有所作为吗?

提前致谢

最佳答案

只需使用:

using(SettingsDialog dialog = new SettingsDialog())
{
    dialog.ShowDialog();
}


这将自动处理dialog

关于c# - 用完表格后需要处理吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7734879/

10-11 01:44