gpt4 book ai didi

C# Form.Close 与 Form.Dispose

转载 作者:IT王子 更新时间:2023-10-29 03:40:55 26 4
gpt4 key购买 nike

我是 C# 的新手,我试图查看之前的帖子,但没有找到好的答案。

在具有单个窗体的 C# Windows 窗体应用程序中,使用 Form.Close() 还是使用 Form.Dispose() 更好?

MSDN 表示对象内的所有资源都已关闭,并且在调用 Close 时会释放表单。尽管如此,我还是在网上遇到了几个遵循 Dispose 而不是 Close 的示例。

一个人比另一个人有优势吗?在哪些情况下我们应该更喜欢其中一种?

最佳答案

This MSDN 上的论坛告诉你。

Form.Close() sends the proper Windows messages to shut down the win32 window. During that process, if the form was not shown modally, Dispose is called on the form. Disposing the form frees up the unmanaged resources that the form is holding onto.

If you do a form1.Show() or Application.Run(new Form1()), Dispose will be called when Close() is called.

However, if you do form1.ShowDialog() to show the form modally, the form will not be disposed, and you'll need to call form1.Dispose() yourself. I believe this is the only time you should worry about disposing the form yourself.

关于C# Form.Close 与 Form.Dispose,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3097364/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com