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?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
System.ValueTypeis not a value-type. Value-types are things that inherit fromValueType, but notValueTypeitself. So:here,
5is loaded on the stack. This is then boxed (with a box of typeint) onto the heap. The object reference is then assigned to the referencefoo. We can see this by looking at the IL for that: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.