【发布时间】:2016-08-10 15:29:56
【问题描述】:
我已经看到开发人员在他们编写的几乎所有方法中都尝试、捕获和 finally,并且他们取消了他们在 finally 块中创建的对象。
例如-
public void Demo()
{
Something s = null;
try
{
s = new Something();
}
catch (Exception ex)
{
throw;
}
finally
{
s = null;
}
}
这样做有什么用吗?这样做的建议是使对象无效,以便 GC 可以收集它们。
如果不这样做,我们可以使用“using”或IDisposable来代替吗?
最佳做法是什么?
【问题讨论】:
标签: c# oop exception-handling