Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>(edited to cover points in comments)</p> <p>To emphasise: there are differences and similarities between value-types and reference-types, but those differences have <strong>nothing</strong> to do with stack vs heap, and everything to do with copy-semantics vs reference-semantics. In particular, if we do:</p> <pre><code>Foo first = new Foo { Bar = 123 }; Foo second = first; </code></pre> <p>Then are "first" and "second" talking about the same copy of <code>Foo</code>? or different copies? It just so happens that the stack is a convenient and efficient way of <em>handling</em> value-types as variables. But that is an implementation detail.</p> <p>(end edit)</p> <p>Re the whole "value types go on the stack" thing... - value types <strong>don't</strong> always go on the stack;</p> <ul> <li>if they are fields on a class</li> <li>if they are boxed</li> <li>if they are "captured variables"</li> <li>if they are in an iterator block</li> </ul> <p>then they go on the heap (the last two are actually just exotic examples of the first)</p> <p>i.e.</p> <pre><code>class Foo { int i; // on the heap } static void Foo() { int i = 0; // on the heap due to capture // ... Action act = delegate {Console.WriteLine(i);}; } static IEnumerable&lt;int&gt; Foo() { int i = 0; // on the heap to do iterator block // yield return i; } </code></pre> <p>Additionally, Eric Lippert (as already noted) has an <a href="http://blogs.msdn.com/ericlippert/archive/2009/04/27/the-stack-is-an-implementation-detail.aspx" rel="noreferrer">excellent blog entry</a> on this subject</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload