Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What are some of the fastest way to read a text file line by line using C#?
There are several fast and efficient ways to read a text file line by line in C#. The most commonly used methods are StreamReader.ReadLine, File.ReadLines, and File.ReadAllLines. Each method has its own advantages depending on the use case and file size.
Using StreamReader.ReadLine
StreamReader is the most memory-efficient approach for reading large files since it processes one line at a time without loading the entire file into memory. This method is ideal for processing very large files where memory usage is a concern.
Example
using System;
using System.IO;
using System.Text;
namespace DemoApplication {
public class Program {
static void Main(string[] args) {
using (var fileStream = File.OpenRead("sample.txt"))
using (var streamReader = new StreamReader(fileStream, Encoding.UTF8)) {
String line;
while ((line = streamReader.ReadLine()) != null) {
Console.WriteLine(line);
}
}
}
}
}
The output of the above code is −
Line 1: Hello World Line 2: This is a sample file Line 3: Reading line by line
Using File.ReadLines
The File.ReadLines method returns an IEnumerable<string> that lazily reads lines from the file. This approach is also memory-efficient as it doesn't load all lines into memory at once, making it suitable for large files while providing cleaner syntax than StreamReader.
Example
using System;
using System.IO;
namespace DemoApplication {
public class Program {
static void Main(string[] args) {
var lines = File.ReadLines("sample.txt");
foreach (var line in lines) {
Console.WriteLine(line);
}
}
}
}
The output of the above code is −
Line 1: Hello World Line 2: This is a sample file Line 3: Reading line by line
Using File.ReadAllLines
File.ReadAllLines reads all lines into a string[] array, loading the entire file content into memory. This method provides random access to lines and is fastest for small to medium-sized files, but should be avoided for large files due to memory consumption.
Example
using System;
using System.IO;
namespace DemoApplication {
public class Program {
static void Main(string[] args) {
var lines = File.ReadAllLines("sample.txt");
for (var i = 0; i < lines.Length; i += 1) {
var line = lines[i];
Console.WriteLine($"Line {i + 1}: {line}");
}
}
}
}
The output of the above code is −
Line 1: Line 1: Hello World Line 2: Line 2: This is a sample file Line 3: Line 3: Reading line by line
Performance Comparison
| Method | Memory Usage | Best For | Random Access |
|---|---|---|---|
| StreamReader.ReadLine | Very Low | Very large files | No |
| File.ReadLines | Low | Large files with clean syntax | No |
| File.ReadAllLines | High | Small files needing random access | Yes |
Asynchronous Reading with StreamReader
For improved performance in I/O bound operations, you can use asynchronous methods −
Example
using System;
using System.IO;
using System.Threading.Tasks;
namespace DemoApplication {
public class Program {
static async Task Main(string[] args) {
using (var streamReader = new StreamReader("sample.txt")) {
string line;
while ((line = await streamReader.ReadLineAsync()) != null) {
Console.WriteLine(line);
}
}
}
}
}
The output of the above code is −
Line 1: Hello World Line 2: This is a sample file Line 3: Reading line by line
Conclusion
For maximum performance with large files, use StreamReader.ReadLine due to its minimal memory footprint. File.ReadLines offers cleaner syntax with similar performance, while File.ReadAllLines is fastest for small files requiring random access to lines.
