Recall & Review
beginner
What is the basic method to read all text from a file in C#?
Use
File.ReadAllText(path) to read the entire content of a text file as a single string.Click to reveal answer
beginner
How can you read a text file line by line in C#?
Use
File.ReadLines(path) or File.ReadAllLines(path) to get each line as a string, allowing you to process the file line by line.Click to reveal answer
beginner
What namespace must you include to work with file reading in C#?
Include
using System.IO; to access file reading methods like File.ReadAllText and File.ReadLines.Click to reveal answer
intermediate
What is the difference between
File.ReadAllText and File.ReadAllLines?File.ReadAllText reads the whole file into one string.<br><br>File.ReadAllLines reads the file into a string array, each element is one line.Click to reveal answer
intermediate
Why should you handle exceptions when reading files in C#?
Because files might not exist, be locked, or have permission issues. Handling exceptions like
FileNotFoundException or IOException prevents crashes and allows graceful error handling.Click to reveal answer
Which method reads the entire content of a text file into a single string?
✗ Incorrect
File.ReadAllText reads the whole file content as one string.Which namespace do you need to include to read files in C#?
✗ Incorrect
System.IO contains classes for file input and output.What does
File.ReadLines return?✗ Incorrect
File.ReadLines returns an IEnumerable<string> that reads lines one by one, which is memory efficient.Which exception should you catch when a file is missing?
✗ Incorrect
FileNotFoundException is thrown when the file path does not exist.What is a good practice when reading files?
✗ Incorrect
Handling exceptions like
IOException helps your program handle errors gracefully.Explain how to read a text file line by line in C# and why it might be better than reading the whole file at once.
Think about memory usage and processing big files.
You got /3 concepts.
Describe the exceptions you should handle when reading a file and why handling them is important.
Consider what can go wrong when accessing files.
You got /4 concepts.