c# - CA2000 : Microsoft.Reliability : Call System.IDisposable.Dispose on object 'dt' before all references to it are out of scope -


i need advice.when run code analysis tool following

warning 1 ca2000 : microsoft.reliability : in method 'class1.test.testmethod()', object 'dt' not disposed along exception paths. call system.idisposable.dispose on object 'dt' before references out of scope. how resolve warnings??

public void testmethod()             {                 datatable dt = new datatable();                 datatable dt1= new datatable();                   try                 {                      if (dt.rows.count == 0)                     {                         dt1.merge(dt);                     }                  }                 catch                 {                     throw;                 }                                 {                     if (dt != null) dt.dispose();                     if (dt1 != null) dt1.dispose();                 }             } 

not sure why getting error, can try using statement block in method , see if error goes away. try like:

public void testmethod() {     using (datatable dt = new datatable())     using (dataview dv = new dataview(dt))     {         //your work     } } 

Comments

Popular posts from this blog

.htaccess - First slash is removed after domain when entering a webpage in the browser -

Automatically create pages in phpfox -

c# - Farseer ContactListener is not working -