Difficulty: Easy
Correct Answer: True
Explanation:
Introduction / Context:
This tests whether you know that string in C# is immutable, whereas StringBuilder provides a mutable buffer for efficient in-place-like modifications.
Given Data / Assumptions:
String are immutable; StringBuilder instances are mutable.
Concept / Approach:
Immutability means any apparent modification of a string produces a new instance, leaving the original unchanged. StringBuilder maintains an internal resizable buffer permitting operations like Append, Insert, and Remove without creating a new string each time (until you call ToString()).
Step-by-Step Solution:
string s = 'a'; then s += 'b'; → returns a new string 'ab'.Create var sb = new StringBuilder('a'); then sb.Append('b'); → same sb buffer now holds 'ab'.
Verification / Alternative check:
Use a memory profiler or check Object.ReferenceEquals before and after modifying a string versus a StringBuilder.
Why Other Options Are Wrong:
Marking the statement as False contradicts the documented behavior of both types.
Common Pitfalls:
Building long strings with + in loops, causing unnecessary allocations; prefer StringBuilder for repeated concatenations.
Final Answer:
True
Discussion & Comments