Bird
0
0

Why does concatenating strings with the + operator inside a loop cause performance issues in C#?

hard🧠 Conceptual Q10 of 15
C Sharp (C#) - Strings and StringBuilder
Why does concatenating strings with the + operator inside a loop cause performance issues in C#?
ABecause strings are immutable and each + creates a new string
BBecause + operator is not supported in loops
CBecause it causes syntax errors
DBecause it modifies the original string in place
Step-by-Step Solution
Solution:
  1. Step 1: Understand string immutability

    Strings in C# cannot be changed after creation; concatenation creates new strings each time.
  2. Step 2: Impact on performance in loops

    Repeated concatenation in loops creates many temporary strings, causing slow performance and memory use.
  3. Final Answer:

    Because strings are immutable and each + creates a new string -> Option A
  4. Quick Check:

    String immutability causes performance issues with + in loops [OK]
Quick Trick: Use StringBuilder for efficient concatenation in loops [OK]
Common Mistakes:
MISTAKES
  • Thinking + is unsupported in loops
  • Assuming syntax errors occur
  • Believing strings change in place

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More C Sharp (C#) Quizzes