這裏我在aspx.cs中使用了dispose方法,但我不確定是否以正確的方式處理對象。我從網上瀏覽它,並在我的代碼中使用它,請你說說對象是以正確格式處理的。這是使用Dispose方法的正確方法嗎?
這是我在我的類文件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;
}
方法:
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;
}
}
處置方法被稱爲在塊的結尾。謝謝你 – JavaH 2012-07-12 06:22:03