c# - 为什么要用 using 语句初始化对话框?

标签 c# coding-style idisposable

我正在改编来自这个来源的编码风格指南: http://www.csharpfriends.com/articles/getarticle.aspx?articleid=336

在“5.2 初始化”下,建议如下:

If you initialize a dialog try to use the using statement:

using (OpenFileDialog openFileDialog = new OpenFileDialog()) { }

选择这种风格的原因是什么?

最佳答案

很可能您只需要短期对话即可立即输入。因此,使用 using 语句,您可以在完成所需的资源后释放资源(处置)。

Using只是在使用后调用 dispose 方法的语法糖。

关于c# - 为什么要用 using 语句初始化对话框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5517974/

相关文章:

python - Python 中类 def 后的空格

c# - 使用隐式类型的局部变量

c# - 实现 IDisposable 接口(interface)的目的是什么?

c# - 我应该如何确保处理可能是一次性的元素?

c# - 首先将外键映射到 EF 代码中的非主代理键列

c# - 如何旋转 4x4 矩阵 (C#/XNA)

visual-studio - Visual Studio : any one click switch between tabs and spaces indentation?

.net - 处置时托管资源和 native 资源有什么区别? (。网)

c# - 使用函数在天蓝色表存储中使用多个查询参数

c# - HTTP Post 切断字符串