Difficulty: Easy
Correct Answer: When it goes out of scope.
Explanation:
Introduction / Context:
Structs are value types. Their lifetime semantics differ from reference types managed by the garbage collector.
Given Data / Assumptions:
Concept / Approach:
A struct variable's storage is typically reclaimed when it goes out of scope (for locals, at the end of the method or block). The GC does not “collect” standalone stack values. If a struct is a field within a reference type, its lifetime follows that containing object. The presence or absence of new only affects initialization, not fundamental lifetime.
Step-by-Step Solution:
Verification / Alternative check:
Inspect generated IL and JIT behavior; locals are allocated in the stack frame and discarded on method exit.
Why Other Options Are Wrong:
Common Pitfalls:
Assuming structs behave like heap objects with GC; they usually do not.
Final Answer:
When it goes out of scope.
Discussion & Comments