Difficulty: Easy
Correct Answer: 2, 3, 5
Explanation:
Introduction / Context:
This item tests understanding of string interning and reference behavior in C#.NET. String literals are interned, which can cause multiple variables initialized with the same literal to point to the same object instance.
Given Data / Assumptions:
s1
and s2
.'Hi'
.new
, and whether references point to the same instance.
Concept / Approach:
In .NET, string literals are interned. Assigning the same literal to two variables does not create two different heap objects; both references point to the interned single instance. Creating strings without new
is perfectly legal through literals.
Step-by-Step Solution:
Verification / Alternative check:
Use Object.ReferenceEquals(s1, s2)
→ returns true
. Using new string('H',1)
etc. would bypass interning unless explicitly interned.
Why Other Options Are Wrong:
Any option including 1 or 4 is incorrect because they contradict interning and literal semantics.
Common Pitfalls:
Confusing content equality with reference equality; assuming every assignment creates a new object on the heap.
Final Answer:
2, 3, 5
Discussion & Comments