10

I know a few differences,

  1. Value types are stored on the stack where as reference types are stored on the managed heap.
  2. Value type variables directly contain their values where as reference variables holds only a reference to the location of the object that is created on the managed heap.

Is there any other difference i missed... If so,what are they?

ACP
  • 34,682
  • 100
  • 231
  • 371
  • 8
    asked many times already on SO.... – Mitch Wheat Mar 10 '10 at 06:20
  • @Wheat, Quickly searching, no, this question hasn't been asked or answered directly. – strager Mar 10 '10 at 06:22
  • 3
    I don't have the time to answer this fully at the moment - but I'd just like to point out that "value types are stored on the stack" is an overstatement. See http://pobox.com/~skeet/csharp/memory.html. For more on reference types vs value types, see http://pobox.com/~skeet/csharp/references.html – Jon Skeet Mar 10 '10 at 06:25
  • @strager - The phrasing is slightly different but the topic is covered in a lot of places. For example http://stackoverflow.com/questions/1130468/memory-allocation-of-value-types-and-reference-types-in-net-framework – Mike Two Mar 10 '10 at 06:37
  • Value types can be stored on the heap when boxed http://msdn.microsoft.com/en-us/library/yz2be5wk.aspx – Zaid Masud May 30 '12 at 09:28

4 Answers4

22

Please read: The stack is an implementation detail, and don't ever again repeat the canard that stack allocation is what differentiates value types from reference types in .NET. The CLR may choose to allocate a variable anywhere it wants to.

The most important difference is in the assignment semantics. When you assign a value type to a variable (or pass it to a method as an argument), all of the data is copied. When you assign a reference type, only a reference is copied - both references point to the same object instance in memory.

software_writer
  • 3,941
  • 9
  • 38
  • 64
Aaronaught
  • 120,909
  • 25
  • 266
  • 342
  • Bit harshly worded but hey that's the best way I learnt, like if you annoyed Gandalf and awaiting him explaining it to you with annoyance in his voice, it tends to stick =D – Paul C Nov 19 '12 at 15:54
15

Here are some additional differences:

  1. Value types cannot be inherited, whereas reference types can
  2. Value types are implemented as a struct, reference types as a class
  3. Value types, by default, cannot be assigned real null values (the ? syntax is a workaround and still doesn't result in a true null value)
  4. Assigning a value type to another variable, or passing it as a parameter in a method, makes a copy of it whereas with a reference type the variable represents the memory location of the object
  5. It is a compile-time error for a struct to declare an explicit parameterless constructor, but the same does not apply to a class
  6. It is a compile-time error to use the "this" object before all fields are assigned in a struct, but the same does not apply to a class
  7. It is a compile-time error for a struct to not fully assign all properties in its constructor, but the same does not apply to a class
Zaid Masud
  • 13,225
  • 9
  • 67
  • 88
2

As mentioned by Aaronaught and Eric blog post:

Remember the rule, Reference types always goes to the Heap, whereas Value Types always go where they were declared? If a Value Type is declared outside of a method, but inside a Reference Type it will be placed within the Reference Type on the Heap.

Bhaskar
  • 10,537
  • 6
  • 53
  • 64
0

i think values are implicitly assigned memory but not reference types they must be assigned memory explicitly

tariq
  • 2,193
  • 15
  • 26