Tutorial :still trying to understand the dispose pattern



Question:

i've read msdn and various posts about dispose pattern, and there are still a couple of things i don't understand. I've written the following code to test the dispose pattern. Please note that there aren't unmanged resources, i'm using vs2008 and .net 3.5 :

public partial class Form1 : Form  {      public Form1()      {          InitializeComponent();      }        private void tryDispose()      {          //test 1 : allocate resource and leave manage it to gc          BL.myclass df = new BL.myclass();          //test 2 : try to force garbage collecting          //GC.Collect();         //test 3 : call dispose myself         //using (BL.myclass df = new BL.myclass())          //{             //}        }        private void button1_Click(object sender, EventArgs e)      {          tryDispose();      }  

this is my disposable class:

class myclass: IDisposable  {        private StronglyTypedDs myDS;      private bool _disposed;        public myclass()      {          using (myDSTableAdapter docDocadpt = new myDSTableAdapter())          {              myDS = new StronglyTypedDs();              docDocadpt.Fill(myDS.TheTable);          }      }          #region IDisposable Members        public void Dispose()      {          Dispose(true);          GC.SuppressFinalize(this);      }        ~myclass()      {          Dispose(false);      }        protected virtual void Dispose(bool disposing)      {          if (!_disposed)          {              if (disposing)              {                  if (myDS != null)                      myDS .Dispose();                  myDS = null;              }          }          _disposed = true;      }          





        
Previous
Next Post »