File class static methods in C Sharp (C#) - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When using static methods of the File class, it is important to understand how the time taken grows as the file size or number of files increases.
We want to know how the work done by these methods changes when the input gets bigger.
Analyze the time complexity of the following code snippet.
string[] lines = File.ReadAllLines("data.txt");
foreach (string line in lines)
{
Console.WriteLine(line);
}
This code reads all lines from a file into an array, then prints each line to the console.
- Primary operation: Reading each line from the file and storing it in an array.
- How many times: Once for each line in the file, repeated for all lines.
- Secondary operation: Looping through the array to print each line, also once per line.
As the number of lines in the file grows, the time to read and print grows roughly the same way.
| Input Size (n lines) | Approx. Operations |
|---|---|
| 10 | About 10 reads and 10 prints |
| 100 | About 100 reads and 100 prints |
| 1000 | About 1000 reads and 1000 prints |
Pattern observation: The total work grows directly with the number of lines; doubling lines doubles work.
Time Complexity: O(n)
This means the time taken grows in a straight line with the number of lines in the file.
[X] Wrong: "Reading a file with File.ReadAllLines is always fast and constant time regardless of file size."
[OK] Correct: The method reads every line, so the time depends on how many lines there are. Bigger files take longer.
Understanding how file reading scales helps you explain performance in real apps, showing you know how input size affects work done.
"What if we used File.ReadLines instead of File.ReadAllLines? How would the time complexity change?"
Practice
File class static methods checks if a file exists at a given path?Solution
Step 1: Understand the purpose of each method
File.Exists(path)checks if the file is present.File.ReadAllText(path)reads file content.File.Delete(path)deletes a file.File.Copy(source, destination)copies a file.Step 2: Identify the method that checks existence
The method that returns a boolean indicating if the file exists isFile.Exists(path).Final Answer:
File.Exists(path) -> Option AQuick Check:
Check file existence = File.Exists(path) [OK]
- Confusing ReadAllText with Exists
- Using Delete to check existence
- Thinking Copy checks file presence
File class?Solution
Step 1: Recall the correct method name for writing text
TheFileclass usesWriteAllTextto write all text to a file at once.Step 2: Check method signatures
WriteText,Write, andWriteLineare not valid static methods ofFileclass.Final Answer:
File.WriteAllText(path, "Hello World") -> Option BQuick Check:
Write text to file = WriteAllText [OK]
- Using non-existent WriteText method
- Confusing File class with StreamWriter methods
- Using WriteLine which is not in File class
string content = File.ReadAllText("test.txt");
Console.WriteLine(content);Solution
Step 1: Understand what File.ReadAllText does
This method reads all text from the specified file and returns it as a string.Step 2: Analyze the code output
The variablecontentwill hold "Hello" from the file. The Console.WriteLine prints this string.Final Answer:
Hello -> Option AQuick Check:
ReadAllText returns file content [OK]
- Printing file name instead of content
- Expecting method name as output
- Assuming error without checking file existence
File.Copy("source.txt", "dest.txt");
File.Delete("source.txt");
File.Copy("source.txt", "dest.txt");Solution
Step 1: Trace the file operations
First, source.txt is copied to dest.txt. Then source.txt is deleted. Finally, the code tries to copy source.txt again.Step 2: Identify the problem
After deletion, source.txt no longer exists, so the second copy call will throw aFileNotFoundException.Final Answer:
Second File.Copy will throw an exception because source.txt was deleted -> Option DQuick Check:
Copy after delete causes error [OK]
- Assuming File.Copy works on deleted files
- Thinking order of Delete and Copy doesn't matter
- Believing .txt files cannot be copied
File class static methods to do this?Solution
Step 1: Understand the requirements
Backup only if original file exists and do not overwrite existing backup file.Step 2: Analyze each option
if (File.Exists("file.txt")) File.Copy("file.txt", "backup.txt", true); overwrites backup.txt because of 'true' overwrite flag. if (File.Exists("file.txt") && !File.Exists("backup.txt")) File.Copy("file.txt", "backup.txt"); checks existence of both files and copies only if backup.txt does not exist. File.Copy("file.txt", "backup.txt"); copies without checks, risking errors or overwrites. File.Copy("file.txt", "backup.txt", false); copies without overwrite but does not check if original file exists.Final Answer:
if (File.Exists("file.txt") && !File.Exists("backup.txt")) File.Copy("file.txt", "backup.txt"); -> Option CQuick Check:
Check both files before copy to avoid overwrite [OK]
- Not checking if backup file exists
- Using overwrite flag incorrectly
- Copying without checking original file existence
