Reputation: 8630
System.ValueType is a class,
yet all value types are structs.
If I create an instance of ValueType like so:
System.ValueType foo = 5;
...is it saved on the heap or on the stack?
Upvotes: 2
Views: 369
Reputation: 1062905
System.ValueType
is not a value-type. Value-types are things that inherit from ValueType
, but not ValueType
itself. So:
System.ValueType foo = 5;
here, 5
is loaded on the stack. This is then boxed (with a box of type int
) onto the heap. The object reference is then assigned to the reference foo
. We can see this by looking at the IL for that:
ldc.i4.5 // push int32 value 5 onto the stack
box int32 // box the value at the top of the stack, type int32
stloc.0 // assign to "foo"
In general, though: structs can be either on the heap or on the stack, depending on the context. Frankly, it doesn't matter which - because both are implementation details. It is the behaviour that matters... and (at least in their unboxed form), the key point about structs is their copy-on-assign semantic.
Upvotes: 4
Reputation: 5801
All value types can be stored on the stack(yes, i itended to say that) but not always, anyway this part where even value types inherit from object is just an exception required because of how the .NET framework is built, this also allowing the preservation of a perfect tree like structure.
For more advanced explainations you can check Eric Lippert's blog.
http://blogs.msdn.com/b/ericlippert/archive/2010/09/30/the-truth-about-value-types.aspx
As a paraprahse from Eric Lippert's blog:
It is usually stated incorrectly: the statement should be "value types can be stored on the stack", instead of the more common "value types are always stored on the stack".
It is almost always irrelevant. We've worked hard to make a managed environment where the distinctions between different kinds of storage are hidden from the user. Unlike some languages, in which you must know whether a particular storage is on the stack or the heap for correctness reasons.
It is incomplete. What about references? References are neither value types nor instances of reference types, but they are values. They've got to be stored somewhere. Do they go on the stack or the heap? Why does no one ever talk about them? Just because they don't have a type in the C# type system is no reason to ignore them.
Upvotes: 2
Reputation: 50114
This post talks about where value types are stored.
The way in the past I've usually pushed back on this myth is to say that the real statement should be "in the Microsoft implementation of C# on the desktop CLR, value types are stored on the stack when the value is a local variable or temporary that is not a closed-over local variable of a lambda or anonymous method, and the method body is not an iterator block, and the jitter chooses to not enregister the value."
This series of posts talks about why you shouldn't really care.
Surely the most relevant fact about value types is not the implementation detail of how they are allocated, but rather the by-design semantic meaning of “value type”, namely that they are always copied “by value”. If the relevant thing was their allocation details then we’d have called them “heap types” and “stack types”. But that’s not relevant most of the time. Most of the time the relevant thing is their copying and identity semantics.
Upvotes: 2
Reputation: 63200
I would say if you had a look at the MSDN page for System.Valuetype
you would have found your answer:
Value types are either stack-allocated or allocated inline in a structure. Reference types are heap-allocated. Both reference and value types are derived from the ultimate base class Object. In cases where it is necessary for a value type to behave like an object, a wrapper that makes the value type look like a reference object is allocated on the heap, and the value type's value is copied into it.
Upvotes: 3
Reputation: 10325
Data types are separated into value types and reference types. Value types are either stack-allocated or allocated inline in a structure. Reference types are heap-allocated.
Source: http://msdn.microsoft.com/en-us/library/system.valuetype.aspx
Upvotes: 2