Where are C#.NET String objects created? Choose the correct statement about the memory location of a String.

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:

  • C# strings are of type System.String, a reference type.
  • The .NET runtime manages memory for reference types on the managed heap.


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:

Option A → False: only references can be on the stack; object data for strings is heap-allocated.Option B → False: size does not change the allocation model for strings.Option C → False: 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.

More Questions from Strings

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion