When I run code analysis tool I get the following:
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();
}
}
object 'dt' is not disposed along all exception paths
I think the compiler is actually complaining about the empty catch block, but using a using statement (like the currently top rated answer) should be the best practice as you don't even need to call dispose then. EDIT: +1 for being bothered about warnings. - Izzy