当我运行代码分析工具时,我得到以下信息:
Warning 1 CA2000 : Microsoft.Reliability : In method 'Class1.test.testMethod()', object 'dt' is not disposed along all exception paths. Call System.IDisposable.Dispose on object 'dt' before all references to it are out of scope. How to resolve the warnings??
public void testMethod()
{
DataTable dt = new DataTable();
DataTable dt1= new DataTable();
try
{
if (dt.Rows.Count == 0)
{
dt1.Merge(dt);
}
}
catch
{
throw;
}
finally
{
if (dt != null) dt.Dispose();
if (dt1 != null) dt1.Dispose();
}
}
最佳答案
不太确定为什么会出现该错误,但您可以尝试 using
方法中的语句 block ,看看错误是否消失了。试一试:
public void testMethod()
{
using (DataTable dt = new DataTable())
using (DataView dv = new DataView(dt))
{
//your work
}
}
关于c# - CA2000 : Microsoft. 可靠性 : Call System. IDisposable.Dispose 在对象 'dt' 之前,对它的所有引用都超出范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16648158/