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
的创建包装在 using
statement. 中
using(ProgressBar pBar = new ProgressBar(obj))
{
if(_FileRead!=false)
{
pBar.Text = langSupport.GetMessages("123", cultureName);
pBar.ShowDialog();
}
}
由于它实现了 IDisposable
,因此这是确保正确处置的最佳方式。
关于c# - 在 C# 中处理资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7037171/