A using statement works on classes that implements the interface IDisposable.
And the dispose method should release all allocated resources, e.g. close open files, delete memory etc.
Implementing your example in two different ways. The end result is the same.
Not using
using
:
SqlDataReader dr = null;
try
{
dr = cmd.ExecuteReader())
{
dr.Read();
n += 1;
idc.Add(dr[0]);
dt.Add(dr[1]);
c1.Add(dr[2]);
c2.Add(dr[3]);
c3.Add(dr[4]);
}
}
finally
{
if (dr != null)
dr.Close();
}
using
used:
using (SqlDataReader dr = cmd.ExecuteReader())
{
dr.Read();
n += 1;
idc.Add(dr[0]);
dt.Add(dr[1]);
c1.Add(dr[2]);
c2.Add(dr[3]);
c3.Add(dr[4]);
}
So you need to implement the IDisposable interface in your class
public class MDFtoMDF : IDisposable
{
public int cid { get; set; }
public string cdt { get; set; }
public string cc1 { get; set; }
public string cc2 { get; set; }
public string cc3 { get; set; }
public void Dispose()
{
Dipsose(true);
}
private void Dispose(bool disposable)
{
if (disposable)
{
}
GC.SuppressFinalize(this);
}
}
Now you can write the code as below
using (MDFtoMDF test = new MDFtoMDF())
{
}
The thing is that if you don't have resources that must be explicitly released, then the using statement does not add any useful functionality.