在 C# 中处置资源
ProgressBar pBar = new ProgressBar(obj);
if(_FileRead!=false)
{
pBar.Text = langSupport.GetMessages("123", cultureName);
pBar.ShowDialog();
}
在此示例中,我如何处置“pBar”资源。下面我指定了 3 种方法,哪种是对象处理的最佳方法?
pBar.Dispose();
pBar = null;
pBar.Dispose();
pBar = null;
ProgressBar pBar = new ProgressBar(obj);
if(_FileRead!=false)
{
pBar.Text = langSupport.GetMessages("123", cultureName);
pBar.ShowDialog();
}
In this example how I can dispose "pBar" resource. Below I have specifide 3 ways, which is the best way of object dispose?
pBar.Dispose();
pBar = null;
pBar.Dispose();
pBar = null;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将
ProgressBar
的创建包装在中
语句。由于它实现了
IDisposable
,因此这是确保正确处置的最佳方式。Wrap the creation of the
ProgressBar
in ausing
statement.Since it implements
IDisposable
, this is the best way to ensure proper disposal.如果它支持它,我会使用:
这样,当它退出使用时,它会处理所有相关对象。
If it supports it I would use:
In that way when it exits the using it disposes of all the relevant objects.