Writing text files in C Sharp (C#) - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When writing text files, it's important to understand how the time it takes grows as the file size grows.
We want to know how the program's work changes when we write more lines or characters.
Analyze the time complexity of the following code snippet.
using System.IO;
string[] lines = new string[] { "Line1", "Line2", "Line3" };
using StreamWriter writer = new StreamWriter("output.txt");
foreach (string line in lines)
{
writer.WriteLine(line);
}
This code writes each line from an array into a text file, one line at a time.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Writing each line to the file inside a loop.
- How many times: Once for each line in the input array.
As the number of lines increases, the program writes more times, so the work grows steadily.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 write operations |
| 100 | 100 write operations |
| 1000 | 1000 write operations |
Pattern observation: The number of write actions grows directly with the number of lines.
Time Complexity: O(n)
This means the time to write grows in a straight line as the number of lines grows.
[X] Wrong: "Writing a file always takes the same time no matter how big it is."
[OK] Correct: Writing more lines means more work, so time grows with file size.
Understanding how file writing time grows helps you explain performance in real programs that save data.
"What if we buffered all lines into one big string and wrote once? How would the time complexity change?"
Practice
File.WriteAllText method do in C#?Solution
Step 1: Understand the purpose of File.WriteAllText
This method is designed to write text to a file, creating it if it doesn't exist or overwriting it if it does.Step 2: Compare with other file methods
Reading text uses File.ReadAllText, appending uses File.AppendAllText, and deleting uses File.Delete, so these are different methods.Final Answer:
It creates a new text file or overwrites an existing one with the specified content. -> Option AQuick Check:
File.WriteAllText writes or overwrites text [OK]
- Confusing WriteAllText with ReadAllText
- Thinking it appends instead of overwriting
- Assuming it deletes files
File.WriteAllText?Solution
Step 1: Check method name and parameters
The correct method is File.WriteAllText with the first parameter as the file path and the second as the text content.Step 2: Validate syntax correctness
File.WriteAllText("greeting.txt", "Hello World"); matches the correct method name and parameter order. Options A, B, and D have incorrect method names or parameter order.Final Answer:
File.WriteAllText("greeting.txt", "Hello World"); -> Option DQuick Check:
Correct method and parameter order = File.WriteAllText("greeting.txt", "Hello World"); [OK]
- Swapping parameters order
- Using incorrect method names like WriteText
- Calling method on WriteAllText instead of File
string path = "notes.txt"; File.WriteAllText(path, "Line 1\nLine 2"); File.WriteAllText(path, "New Line");
Solution
Step 1: Analyze first File.WriteAllText call
The first call writes "Line 1\nLine 2" to "notes.txt", creating or overwriting the file.Step 2: Analyze second File.WriteAllText call
The second call overwrites the entire file content with "New Line", replacing previous text.Final Answer:
New Line -> Option AQuick Check:
Second WriteAllText overwrites file content [OK]
- Assuming text appends instead of overwriting
- Thinking both texts combine in file
- Ignoring the second WriteAllText call
File.WriteAllText(output.txt, "Data");
Solution
Step 1: Check the file path parameter
The file path must be a string, so it needs quotes around it. Here, output.txt is unquoted, causing a syntax error.Step 2: Verify method name and parameter type
The method name is correct, and it accepts strings. Absolute path is not mandatory; relative paths work fine.Final Answer:
Missing quotes around the file name output.txt -> Option BQuick Check:
File path must be a string literal [OK]
- Forgetting quotes around file path
- Mixing method name order
- Thinking absolute path is required
lines to a file named "log.txt" so that each element appears on its own line. Which code snippet correctly does this using File.WriteAllText?Solution
Step 1: Understand the input and desired output
We have a string array lines and want each element on its own line in the file.Step 2: Check how to convert array to single string with line breaks
Using string.Join("\n", lines) joins array elements with newline characters, creating the correct multiline string.Step 3: Validate other options
lines.ToString() returns type name, not content; passing array directly is invalid; string.Concat joins without separators.Final Answer:
File.WriteAllText("log.txt", string.Join("\n", lines)); -> Option CQuick Check:
Join array with \n for multiline text [OK]
- Passing array directly instead of a string
- Using ToString() on array expecting content
- Concatenating without separators
