0
0
C Sharp (C#)programming~5 mins

StreamReader and StreamWriter in C Sharp (C#) - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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#?
AFileStreamWriter
BStreamWriter
CTextWriter
DStreamReader
What keyword helps ensure StreamReader or StreamWriter is closed automatically?
Ausing
Btry
Clock
Dawait
What exception is thrown if you read from a closed StreamReader?
AObjectDisposedException
BNullReferenceException
CFileNotFoundException
DIOException
Which method writes a line of text to a StreamWriter?
AReadLine()
BWriteLine()
CWriteText()
DAppendLine()
What is the default encoding used by StreamReader and StreamWriter if not specified?
AUTF-16
BASCII
CUTF-8
DISO-8859-1
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.