title: struct with reference members : heap or stack?
body: <p>It is common knowledge that a <code>struct</code> is a <a href="http://msdn.microsoft.com/en-us/library/34yytbws%28v=vs.90%29.aspx" rel="noreferrer">value type</a>, and is therefore allocated on the stack (except for specific cases where it is boxed in a class for instance).</p>
  
  <p>However let's consider this <code>struct</code>:</p>
  
  <pre><code>public struct TestStruct
  {
      public List&lt;int&gt; items;
  }
  
  internal class Program
  {
      private static void Main(string[] args)
      {
          TestStruct g;
      }
  }
  </code></pre>
  
  <p>Our <code>TestStruct</code> <code>g</code> is not a member of a class, but a "standalone" variable declared in the <code>Main</code> function. It fits the requirements of a <code>stack</code>-allocated variable.</p>
  
  <p>However, if I write:</p>
  
  <pre><code>g.items = new List&lt;int&gt;();
  </code></pre>
  
  <ol>
  <li>I suppose <code>items</code> is allocated on the <code>heap</code> isn't it? Does <code>g</code> "go"
  on the <code>heap</code> as well?</li>
  <li>What happens for <code>items</code> when <code>g</code> goes out of scope (i.e. does the
  <code>GC</code> have to do its job for <code>items</code>)?</li>
  <li>What if instead of a <code>List&lt;int&gt;</code> we had a variable of type
  implementing <code>IDisposable</code>, what would be the best course of action?</li>
  </ol>
  
  <p>PS: I know one could (should?) use a <code>class</code> in this case instead of a <code>struct</code>. I'm just being confused by this specific case.</p>