Difficulty: Easy
Correct Answer: A String is created on the heap.
Explanation:
Introduction / Context:
Understanding where objects live clarifies performance and lifetime semantics in .NET. Strings in C# are reference types managed by the garbage collector.
Given Data / Assumptions:
System.String
, a reference type.
Concept / Approach:
Local variables may be stored on the stack, but the data for reference type instances (like strings) is allocated on the heap. The variable holds a reference to that heap object. String is not a primitive in C#; it is a reference type with special language support.
Step-by-Step Solution:
string
is not a primitive in C#.Option D → Incorrect syntax and unnecessary; typical construction uses literals or new string(char[])
overloads.Option E → True: String instances are on the heap.
Verification / Alternative check:
Inspect IL or use a debugger: references are local; object memory is heap-based.
Why Other Options Are Wrong:
They either misuse syntax or conflate variable storage (references) with object storage (heap).
Common Pitfalls:
Assuming small objects are placed on the stack automatically; in .NET, object allocation strategy depends on type category, not just size.
Final Answer:
A String is created on the heap.
Discussion & Comments