title: Count Instances of Class
body: <p>I am looking for a way to programatically get the number of instances of a certain class type in .NET and Java.</p>
  
  <p>Say for example I have class Foo.  I want to be able to, in the same process, get a current count of all instance of Foo.<br>
  However I cannot modify Foo, so a static int with counting is out.  Also I cannot just add all instances I make to some static list and count that.  I want to be able to just say:</p>
  
  <pre><code>System.GC.numberOf&lt; Foo &gt;()
  </code></pre>
  
  <p>or something.</p>
  
  <p>I was looking through the garbage collectors but I could not find any relevant methods.</p>