Recall & Review
beginner
What is StreamReader used for in C#?
StreamReader is used to read characters from a byte stream, typically from files, in a simple and efficient way.
Click to reveal answer
beginner
What is the main purpose of StreamWriter in C#?
StreamWriter is used to write characters to a stream, usually to create or modify text files.
Click to reveal answer
intermediate
How do you properly close a StreamReader or StreamWriter after use?
You should call the
Dispose() method or use a using statement to automatically close and release resources.Click to reveal answer
intermediate
What happens if you try to read from a StreamReader after it is closed?
An
ObjectDisposedException is thrown because the stream is no longer available for reading.Click to reveal answer
beginner
Show a simple example of writing text to a file using StreamWriter.
Example:<br>
using (var writer = new StreamWriter("file.txt")) {
writer.WriteLine("Hello, world!");
}Click to reveal answer
Which class would you use to read text from a file in C#?
✗ Incorrect
StreamReader is designed specifically for reading text from streams like files.
What keyword helps ensure StreamReader or StreamWriter is closed automatically?
✗ Incorrect
The 'using' statement automatically disposes the object when done.
What exception is thrown if you read from a closed StreamReader?
✗ Incorrect
Reading from a closed stream throws ObjectDisposedException.
Which method writes a line of text to a StreamWriter?
✗ Incorrect
WriteLine() writes a line of text and adds a newline.
What is the default encoding used by StreamReader and StreamWriter if not specified?
✗ Incorrect
UTF-8 is the default encoding for StreamReader and StreamWriter.
Explain how to read all lines from a text file using StreamReader.
Think about using a 'using' block and reading line by line.
You got /3 concepts.
Describe how to write multiple lines to a file using StreamWriter safely.
Focus on resource management and writing lines.
You got /3 concepts.