Programming in C# — Question 10
You need to write a method that combines an unknown number of strings. The solution must minimize the amount of memory used by the method when the method executes.
What should you include in the code?
Answer options
- A. The String.Concat method
- B. The StringBuilder.Append method
- C. The + operator
- D. The += operator
Correct answer: A
Explanation
The String.Concat method is efficient for combining multiple strings without creating unnecessary intermediate objects, which minimizes memory usage. In contrast, the StringBuilder.Append method is also efficient but not as direct for this specific task. The + operator and the += operator both create multiple temporary string objects, leading to higher memory consumption.