title: Testing/Verifying a WeakReference
body: <p>I'd like to verify that code setting up a <a href="http://msdn.microsoft.com/en-us/library/system.weakreference.aspx" rel="noreferrer"><code>WeakReference</code></a> does not accidentally hold a strong reference to the referenced object. (Here's <a href="https://stackoverflow.com/questions/3260211/one-liner-weakreference-to-a-lambda-event-handler">an example</a> of how it is easy to accidentally do this.)</p>
  
  <p>Does this look like the best way to check for inadvertent strong references?</p>
  
  <pre><code>TestObject testObj = new TestObject();
  WeakReference wr = new WeakReference(testObj);
  
  // Verify that the WeakReference actually points to the intended object instance.
  Assert.Equals(wr.Target, testObject);
  
  // Force disposal of testObj;
  testObj = null;
  GC.Collect();
  // If no strong references are left to the wr.Target, wr.IsAlive will return false.
  Assert.False(wr.IsAlive);
  </code></pre>