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

Reading text files in C Sharp (C#) - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
AFile.ReadAllText
BFile.ReadLines
CFile.ReadAllLines
DFile.WriteAllText
Which namespace do you need to include to read files in C#?
ASystem.Text
BSystem.IO
CSystem.Net
DSystem.Collections
What does File.ReadLines return?
AA byte array of the file content
BAn array of strings, each representing a line
CA single string with all file content
DAn IEnumerable&lt;string&gt; to read lines lazily
Which exception should you catch when a file is missing?
AFileNotFoundException
BArgumentNullException
CIndexOutOfRangeException
DFormatException
What is a good practice when reading files?
AIgnore exceptions to keep code simple
BAlways read files without checking if they exist
CHandle exceptions to avoid program crashes
DUse <code>Console.ReadLine()</code> to read files
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.