Reading text files in C Sharp (C#) - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When reading text files, it's important to know how the time it takes grows as the file gets bigger.
We want to understand how the program's work changes when the file size changes.
Analyze the time complexity of the following code snippet.
using System.IO;
string[] lines = File.ReadAllLines("file.txt");
foreach (string line in lines)
{
Console.WriteLine(line);
}
This code reads all lines from a text file into an array, then prints each line.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Reading each line from the file and printing it.
- How many times: Once for each line in the file.
As the number of lines grows, the program does more work roughly in direct proportion.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 reads and prints |
| 100 | About 100 reads and prints |
| 1000 | About 1000 reads and prints |
Pattern observation: The work grows evenly as the file gets bigger.
Time Complexity: O(n)
This means the time to read and print grows directly with the number of lines in the file.
[X] Wrong: "Reading a file is always instant, so time doesn't grow with file size."
[OK] Correct: Reading each line takes time, so bigger files take longer because more lines must be processed.
Understanding how file reading time grows helps you write efficient programs and answer questions about handling large data.
"What if we read the file line by line using a stream instead of reading all lines at once? How would the time complexity change?"
Practice
File.ReadAllLines method do in C#?Solution
Step 1: Understand the method purpose
File.ReadAllLinesreads a text file and returns each line as an element in a string array.Step 2: Compare with other methods
File.ReadAllTextreturns the whole file as one string, not an array of lines.Final Answer:
Reads all lines from a text file and returns them as a string array. -> Option BQuick Check:
ReadAllLines = string array [OK]
- Confusing ReadAllLines with ReadAllText
- Thinking it writes to a file
- Assuming it deletes files
File.ReadAllText?Solution
Step 1: Identify method return types
File.ReadAllTextreturns a single string, so the variable must be string.Step 2: Match syntax with variable type
string content = File.ReadAllText("data.txt"); uses string variable with ReadAllText correctly. Options B and D mismatch method and variable types. string[] content = File.ReadAllText("data.txt"); tries to assign string to string array.Final Answer:
string content = File.ReadAllText("data.txt"); -> Option AQuick Check:
ReadAllText returns string, so variable is string [OK]
- Assigning ReadAllText to string array
- Using ReadAllLines but expecting string
- Wrong variable type for method
string[] lines = File.ReadAllLines("example.txt");
Console.WriteLine(lines.Length);Solution
Step 1: Understand ReadAllLines output
The method returns an array with one element per line in the file. Here, 3 lines means array length is 3.Step 2: Analyze Console.WriteLine output
Printinglines.Lengthoutputs the number of lines, which is 3.Final Answer:
3 -> Option AQuick Check:
Array length = number of lines = 3 [OK]
- Expecting content printed instead of length
- Confusing ReadAllText with ReadAllLines
- Assuming runtime error without cause
string[] lines = File.ReadAllLines(notes.txt);
foreach (string line in lines)
{
Console.WriteLine(line);
}Solution
Step 1: Check file path syntax
The file name must be a string literal, so it needs quotes: "notes.txt".Step 2: Verify other code parts
File.ReadAllLinesreturns string array, foreach syntax is correct, and Console.WriteLine can print strings.Final Answer:
Missing quotes around the file name in ReadAllLines. -> Option DQuick Check:
File path must be in quotes [OK]
- Forgetting quotes around file path
- Thinking ReadAllLines returns string
- Misunderstanding foreach syntax
Solution
Step 1: Choose method to read lines
File.ReadAllLinesreturns an array of lines, perfect for line-by-line processing.Step 2: Count lines with "error" case-insensitive
var lines = File.ReadAllLines("log.txt"); int count = lines.Count(line => line.ToLower().Contains("error")); converts each line to lowercase and checks if it contains "error", then counts matches using LINQ.Step 3: Check other options
string content = File.ReadAllText("log.txt"); int count = content.Split('\n').Count(line => line.Contains("error")); uses ReadAllText but misses case-insensitive check. var lines = File.ReadAllLines("log.txt"); int count = 0; foreach(var line in lines) { if(line.Contains("error")) count++; } misses case-insensitive check. string[] lines = File.ReadAllText("log.txt").Split('\n'); int count = lines.Count(line => line.Contains("error")); incorrectly assigns ReadAllText to string array without ToLower.Final Answer:
var lines = File.ReadAllLines("log.txt"); int count = lines.Count(line => line.ToLower().Contains("error")); -> Option CQuick Check:
Use ReadAllLines + ToLower + Count for case-insensitive search [OK]
- Ignoring case sensitivity
- Using ReadAllText but treating as array
- Not converting lines to lowercase
