Bird
Raised Fist0

Which of the following is the correct syntax to write text to a file using the File class?

easy📝 Syntax Q12 of Q15
C Sharp (C#) - File IO
Which of the following is the correct syntax to write text to a file using the File class?
AFile.WriteText(path, "Hello World");
BFile.WriteAllText(path, "Hello World");
CFile.Write(path, "Hello World");
DFile.WriteLine(path, "Hello World");
Step-by-Step Solution
Solution:
  1. Step 1: Recall the correct method name for writing text

    The File class uses WriteAllText to write all text to a file at once.
  2. Step 2: Check method signatures

    WriteText, Write, and WriteLine are not valid static methods of File class.
  3. Final Answer:

    File.WriteAllText(path, "Hello World") -> Option B
  4. Quick Check:

    Write text to file = WriteAllText [OK]
Quick Trick: Use WriteAllText to write full text at once [OK]
Common Mistakes:
MISTAKES
  • Using non-existent WriteText method
  • Confusing File class with StreamWriter methods
  • Using WriteLine which is not in File class

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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