Bird
Raised Fist0

How can you write a list of strings to a file, each on its own line, using File.WriteAllText?

hard🚀 Application Q9 of Q15
C Sharp (C#) - File IO
How can you write a list of strings to a file, each on its own line, using File.WriteAllText?
AJoin the list with newline characters and write once
BCall WriteAllText for each string in a loop
CUse WriteAllText with the list directly
DConvert list to JSON and write to file
Step-by-Step Solution
Solution:
  1. Step 1: Understand WriteAllText expects a single string

    You must convert the list to one string with newlines.
  2. Step 2: Use string.Join to combine list with newlines

    Joining with "\n" creates a single string with lines separated.
  3. Final Answer:

    Join list with newlines and write once. -> Option A
  4. Quick Check:

    Join list before writing = Join the list with newline characters and write once [OK]
Quick Trick: Use string.Join("\n", list) before WriteAllText [OK]
Common Mistakes:
MISTAKES
  • Calling WriteAllText repeatedly overwriting file
  • Passing list directly to WriteAllText
  • Writing JSON instead of plain lines

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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