title: How destructor called when Garbage Collection is in action [.NET]?
body: <p>I was experimenting with garbage collector and found strange count <code>iCnt</code> on <code>destructor</code> call.
  Here is the Code:- </p>
  
  <pre><code>public class MyClass
  {
      static long iCnt;
      public MyClass()
      {
          iCnt++;
      }
  
      ~MyClass()
      {
          iCnt--;
      }
      static public string ObjectCount
      {
          get
          {
              return iCnt.ToString();
          }
      }
  }
  </code></pre>
  
  <p>Below code is to create objects, delete objects(De-refernce by assigning to <code>null</code>) and explicitly call <code>GC.Collect()</code>.
  I have having all it in a Win Form which has <code>Timer</code> of <code>interval = 100</code>, timer show the current count of <code>iCnt</code>.</p>
  
  <pre><code>public partial class Form1 : Form
  {
      MyClass[] Objs;
      public Form1()
      {
          InitializeComponent();
      }
  
      private void btnCreateObjects_Click(object sender, EventArgs e)
      {
          Objs = new MyClass[1000];
          for (int i = 0; i &lt; 1000; i++)
          {
              Objs[i] = new MyClass();
          }
      }
  
      private void timer1_Tick(object sender, EventArgs e)
      {
          label2.Text = MyClass.ObjectCount;
      }
      private void btnDeleteObjects_Click(object sender, EventArgs e)
      {
          for (int i = 0; i &lt; 1000; i++)
          {
              Objs[i] = null;
          }
      }
  
      private void btnInvokeGC_Click(object sender, EventArgs e)
      {
          GC.Collect();
      }
  }
  </code></pre>
  
  <p>Now create the object until the garbage collector is called automatically(for me it took 24 clicks). Again repeat this action.</p>
  
  <p>I saw this <code>iCnt</code> &lt; 1000. which keeps me confusing how the destructor called in this scenario.
  when <code>iCnt</code> &lt; 1000, click on delete object which deference the <code>Objs[1000]</code>. Then call <code>GC.Collect()</code> which makes count of <code>iCnt</code> &lt; 0 (very strange).</p>
  
  <p>Can anybody explain me this behavior. Thanks in advance.</p>