Here i used the dispose method in aspx.cs, but i am not sure wheather the way of disposing the objects is done it in correct way.
Obvisoly i surf it from net and used it in my code,can u please say wheather the objects is disposing in correct format.
This is my method in aspx.cs
public List<CausesField> list(DataTable dt)
{
List<CausesField> lst = new List<CausesField>();
foreach (DataRow row in dt.Rows)
{
using (CausesField SignUpDetails = new CausesField())
{
SignUpDetails.FirstName = row["FirstName"].ToString();
SignUpDetails.LastName = row["LastName"].ToString();
SignUpDetails.Birthdate = row["BirthDay"].ToString();
SignUpDetails.Gender = row["Gender"].ToString();
lst.Add(SignUpDetails);
SignUpDetails.Dispose();
}
}
return lst;
}
My Class file:
public class CausesField:IDisposable
{
public string FirstName { get; set; } public string LastName { get; set; }
public string Birthdate { get; set; } public string Gender { get; set; }
private Component component = new Component();
private bool disposed = false;
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
private void Dispose(bool disposing)
{
// Check to see if Dispose has already been called.
if (!this.disposed)
{
// If disposing equals true, dispose all managed
// and unmanaged resources.
if (disposing)
{
// Dispose managed resources.
component.Dispose();
}
}
disposed = true;
}
}
The
usingstatement automatically callsDisposeat the end of the block, so you should not call it yourself again.Also, in general, unless you are holding onto some external resources (locks, file handles, sockets, database connections, etc.) or unmanaged memory (and I suspect this to be the case in your case), you should not have a finalizer or a
Disposemethod. The garbage collector takes care of freeing up managed memory.