title: GC doesn't collect when WeakReference references a delegate?
body: <p>It seems to me that when I use the <code>WeakReference</code> class on a delegate method of an object class, the object class gets collected by the GC but there is still another copy of it residing in the <code>WeakReference</code>?</p>
  
  <p>I find it difficult to explain in words. I will give an example. I have the following object class called <code>TestObject</code>:</p>
  
  <pre><code>class TestObject
  {
      public string message = "";
  
      private delegate string deleg();
  
      public TestObject(string msg)
      {
          message = msg;
      }
  
      public Delegate GetMethod()
      {
          deleg tmp = this.TestMethod;
          return tmp;
      }
  
      public string TestMethod()
      {
          return message;
      }
  
  }
  </code></pre>
  
  <p>Now, in my main application, I attempt to refer to the method <code>TestMethod</code> in <code>TestObject</code> via a <code>WeakReference</code>. The intention is so that the <code>TestObject</code> can be collected by GC when all hard references are gone. This is how my main application looks like:</p>
  
  <pre><code>static void Main(string[] args)
      {
          var list = new List&lt;WeakReference&gt;();
          var obj = new TestObject("Hello 1");
          list.Add(new WeakReference(obj.GetMethod()));
          Console.WriteLine("Initial obj: " + ((Delegate)list[0].Target).DynamicInvoke());      //Works fine
          obj = null;     //Now, obj is set to null, the TestObject("Hello 1") can be collected by GC
          GC.Collect();   //Force GC
          Console.WriteLine("Is obj null: " + ((obj) == null ? "True" : "False"));
          Console.WriteLine("After GC collection: " + ((Delegate)list[0].Target).DynamicInvoke());
          Console.ReadKey();
      }
  </code></pre>
  
  <p>This is the output when I run the above code:</p>
  
  <p><img src="https://i.stack.imgur.com/Jcohi.png" alt="enter image description here"></p>
  
  <p>Here's the weird thing. <em>On the first line</em>, <code>obj</code> could print <code>"Hello 1"</code> because it was just initialised and <code>obj</code> was holding a reference to the <code>TestObject</code>. All correct. Next, <code>obj</code> was set to <code>null</code> with <code>obj = null</code> and GC was forced to collect. So, <em>in the second line of the output</em>, <code>obj</code> is <code>true</code> to be null. Finally <em>on the last line</em>, since the GC has collected the <code>obj</code> away, I'm expecting it to either throw a <code>NullReferenceException</code> or just print nothing on the output. However, it actually printed the same thing as on the first line of the output! Shouldn't the <code>TestObject</code> be collected by the GC at this point already?!</p>
  
  <p>This begs the question if the <code>TestObject</code> that was first held in <code>obj</code> was later collected by the GC or not after I set obj to null.</p>
  
  <p>If I had passed the whole object into <code>WeakReference</code>, ie, <code>new WeakReference(obj)</code>, instead of a delegate into the <code>WeakReference</code>, it would have worked perfectly. </p>
  
  <p>Unfortunately, in my code, I need to pass into the <code>WeakReference</code> a delegate. How can I have the <code>WeakReference</code> to work correctly so that the GC can collect away the object by only having referenced to a delegate?</p>