First method is to use the “Using” keyword which is quite clean and simple
using (IDisposable obj = new DataTable())
{
//do your logic with obj variable
}
Second method is to use Try and finally keyword
IDisposable obj = null;
try
{
obj = new DataTable();
}
finally
{
if (obj != null)
{
obj.Dispose();
}
}