Difficulty: Easy
Correct Answer: False
Explanation:
Introduction / Context:
This statement conflates the concepts of value vs reference types with stack vs heap storage. In C#, structs are value types, but where their storage resides depends on context, not on the type alone.
Given Data / Assumptions:
Concept / Approach:
A struct is a value type, meaning its data is stored inline where the variable lives. For a local variable, that is usually on the stack. For a field inside a class object, the struct’s data is part of that object, which resides on the heap. When a struct is boxed, a new object is created on the heap containing a copy of the value. Therefore, it is incorrect to claim struct storage is always on the stack.
Step-by-Step Solution:
Verification / Alternative check:
Inspect memory behavior: creating a List<MyStruct> stores struct elements inside an array object on the heap, illustrating context-driven storage.
Why Other Options Are Wrong:
Common Pitfalls:
Equating “value type” with “stack allocation” universally. In .NET, storage location depends on variable lifetime/context, not only on type category.
Final Answer:
False
Discussion & Comments