c# - Why are structs stored on the stack while classes get stored on the heap(.NET)? -


"itemprop =" text ">

I know that one of the differences between classes and structs is that the struct examples are stack and square examples (objects) Stored on that stack stored on

Since classes and stranks are very similar. Does anyone know the difference for this particular glory?

itemprop = "text">

(edit to cover points in the comment)

After emphasizing : There are differences and similarities between the value-type and context-type, but those differences are nothing with heaps of verses , and everything has to do with copy-meaning science reference versus -Semantics Specifically, if we do:

  fu first = new fu {bar = 123}; Fu seconds = First;  

Then talking about the same copy of "first" and "second" foo ? Or different copies? It just happens that the stack handling is a convenient and effective method of value-type in the form of value. But this is an implementation description.

(end edit)

The whole "Go to Value Type Stack" thing ... - Value type No <

  • If they are a class
  • If they are boxing "
  • if they are capturing" the variable
  • if they are in an Iterator block
  • Then they go to the heap (the last two are actually only foreign examples of the first)

    i.e.

      square fu {int i; // Stack on static zero Foo () {int i = 0; // on the heap to capture // ... Action series = Representative {Console.WriteLine (i);}}} Fixed IEnumerable & lt; int & gt; Foo () {// i.I. // // return yield on the heap to block the heater i < In addition to this, Eric Lipert (as previously mentioned) is on this topic 


  • Comments