In C#, understanding how to count lines in a text file efficiently is a fundamental skill. This seemingly simple task is essential, especially in data manipulation, analysis, or validation scenarios. Whether we are dealing with log files, configuration files, or large datasets, having the ability to determine the number of lines in a text file can provide valuable insights into the structure and content of the file.
In this article, we will delve into three distinct approaches that we can employ to count the number of lines in a text file using C#.
Before we dive into this topic, we recommend going through our article C# Back to Basics – Files, StreamWriter and StreamReader.
With that, let’s start.
Get the Number of Lines in the Text File Using File.ReadAllLines Method
In the landscape of C# file handling, the File.ReadAllLines()
method is a straightforward solution for reading all lines from a text file. This method actively engages with the file system, allowing us to retrieve an array containing all the lines from the specified file efficiently.
When we invoke the File.ReadAllLines()
method, the method opens the file, reads all its lines, and then closes the file. The result is an array of strings, where each element represents a line from the file. This direct approach simplifies the code structure and provides a clear path to obtaining the entire content of the file in a single command.
We’ll generate a text file within the project folder. Feel free to check out the code to take a look at the text file.
Next, let’s define a method to count the number of lines in a text file:
public class FileHelper { public static int CountLinesUsingReadAllLinesMethod(string fileName) { if (!File.Exists(fileName)) { throw new FileNotFoundException($"File not found: {fileName}"); } var numberOfLines = File.ReadAllLines(fileName).Length; return numberOfLines; } }
We create a FileHelper
class and define a CountLinesUsingReadAllLinesMethod()
method that accepts fileName
as the input parameter.
As a first step, we check if the file exists using the File.Exists()
method.
Subsequently, we call upon the File.ReadAllLines()
method, which extracts all lines from the designated file. This operation produces an array of strings, with each element representing a distinct line within the file. Following that, we utilize the array’s Length
property to retrieve the overall count of lines.
Now, let’s invoke the method:
const string FileName = "Sample.txt"; FileHelper.CountLinesUsingReadAllLinesMethod(FileName)
Here, we call the CountLinesUsingReadAllLinesMethod()
method by passing the FileName
to get the total number of lines in the text file.
Get the Number of Lines in the Text File Using a StreamReader
The StreamReader
class provides more control and flexibility when dealing with file input. We use the StreamReader
to read characters, lines, or files efficiently. It actively engages with the file, allowing us to navigate through its content in a controlled manner.
When we initialize a StreamReader
, we create an object that facilitates sequential access to a file’s content. This sequential access enables us to read lines individually and perform actions or checks during the reading process.
With that said, let’s create a method inside the FileHelper
class:
public static int CountLinesUsingStreamReader(string fileName) { if (!File.Exists(fileName)) { throw new FileNotFoundException($"File not found: {fileName}"); } var lineCount = 0; using StreamReader reader = new(fileName); while (reader.ReadLine() is not null) { lineCount++; } return lineCount; }
Initially, we declare a lineCount
variable, which we use to keep track of the number of lines in the file. Subsequently, we utilize the using
statement to create and automatically dispose of a StreamReader
instance, and we initialize the StreamReader
with the specified file.
Following that, we run a While
loop to iterate through each line in the file, and within the loop, for each successful line read, we increment the lineCount
variable, which represents the total number of lines in the specified text file.
Now, let’s call the CountLinesUsingStreamReader()
method:
FileHelper.CountLinesUsingStreamReader(FileName);
We invoke the method to get the total number of lines in the text file.
Get the Number of Lines in the Text File Using File.ReadLines Method
The File.ReadLines()
method is a part of the System.IO
namespace in C#, and we use it to read lines from a file efficiently. Unlike the File.ReadAllLines()
method, which loads the entire file into memory, File.ReadLines()
method returns an enumerable collection of strings representing each line in the file. This makes it suitable for scenarios where memory efficiency is a concern, especially when dealing with large files.
Let’s create a method:
public static int CountLinesUsingReadLinesMethod(string fileName) { if (!File.Exists(fileName)) { throw new FileNotFoundException($"File not found: {fileName}"); } IEnumerable<string> lines = File.ReadLines(fileName); return lines.Count(); }
Here, we define a CountLinesUsingReadLinesMethod()
method that takes fileName
as the input parameter.
Next, we invoke the File.ReadLines()
method, which returns an enumerable collection of lines from the specified file. Following this, we use the Count()
method from LINQ to determine the total number of lines in the file.
Now, let’s invoke the method:
FileHelper.CountLinesUsingReadLinesMethod(FileName)
Similarly, we invoke the CountLinesUsingReadLinesMethod()
method.
Benchmark Comparison of Line Counting Methods
Let’s evaluate the three approaches for counting the number of lines in a text file we’ve covered so far. We will perform a benchmark with the BenchmarkDotNet library to measure the time performance for three approaches.
Let’s assess the benchmark results with the SmallFile.txt
file:
| Method | Mean | Error | StdDev | Allocated | |---------------------------------- |---------:|---------:|---------:|----------:| | CountLinesUsingStreamReader | 57.25 us | 0.398 us | 0.332 us | 8.76 KB | | CountLinesUsingReadLines | 57.90 us | 1.148 us | 1.073 us | 8.81 KB | | CountLinesUsingReadAllLines | 59.05 us | 1.013 us | 1.636 us | 9.01 KB |
In examining the benchmark results for the three line-counting methods, we observe nuanced differences in their mean execution times.
The CountLinesUsingStreamReader()
method demonstrates a slightly superior mean performance compared to the other two methods.
However, the variations among the methods are marginal, indicating comparable efficiency.
Now, let’s assess the benchmark results with the LargeFile.txt
file – this file contains a mix of large rows (more than 40 thousand chars in length) and some empty lines:
| Method | Mean | Error | StdDev | Median | Allocated | |---------------------------------- |-----------:|---------:|---------:|-----------:|----------:| | CountLinesUsingReadAllLines | 982.2 us | 24.20 us | 68.65 us | 955.9 us | 1.07 MB | | CountLinesUsingStreamReader | 1,058.4 us | 19.83 us | 21.22 us | 1,055.9 us | 1.06 MB | | CountLinesUsingReadLines | 1,060.2 us | 20.71 us | 21.26 us | 1,060.0 us | 1.06 MB |
In the initial iterations, the CountLinesUsingReadAllLines()
method exhibits a faster mean execution time compared to the other methods.
Comparing the Three Approaches
Let’s break down the comparison of these three methods for counting lines in a text file.
So, we know the first one, using the File.ReadAllLines()
method? It’s so simple. We throw the entire file into memory, which is excellent for small files but could be better if our file is massive. It’s like grabbing a whole book when we only need to check a few pages.
Now, the second one is using a StreamReader
class. That’s for the big leagues. It sequentially reads lines, avoiding loading the entire file into memory. Great for those hefty files where we want to be efficient and not bog down your system.
What about the File.ReadLines()
method? It’s like the Goldilocks solution – not too memory-hungry or slow. It reads lines on-demand, perfect for those large files. Plus, we can do real-time processing without sweating over memory consumption.
Conclusion
So, if we are into simplicity and our file isn’t a monster, we can go for File.ReadAllLines() method. Need to flex some muscles with a big file? StreamReader class is our go-to. But if we want the best of both worlds – large files and real-time processing – give File.ReadLines() method a shot.
In this article, we have explored various approaches to get the total number of lines from a text file.