C# Line Count for FileCount lines in files with StreamReader and a while-loop with a counter.
Strings often contain many lines. It is sometimes necessary to count these lines. This is needed for server logs and CSV files. We can use Regex and string-handling methods. For files, we can use StreamReader and ReadLine.StreamReaderReadLine, ReadLineAsync
First, when using large files it is more memory-efficient not to store the entire file contents in RAM at once. And for web server log files, you need efficiency. This next block of code counts the lines in a file on the disk.File
Tip: It does this by using the parameterless ReadLine instance method offered by the StreamReader class in the System.IO namespace.
CountLinesInFile: This method is static because it stores no state. It contains the logic for counting lines in a file.Static
And: It uses StreamReader, which is a useful class for reading in files quickly. It has the useful ReadLine method.
Next: It increments the count, which contains the number of lines in the file. Finally the int containing the count is returned.
C# program that counts file lines
static void Main()
/// Count the number of lines in the file specified.
/// <param name="f">The filename to count lines.</param>
/// <returns>The number of lines in the file.</returns>
static long CountLinesInFile(string f)
long count = 0;
using (StreamReader r = new StreamReader(f))
while ((line = r.ReadLine()) != null)
(Number of lines in text.txt)
Next we look at the string-based method. Also, we see the regular expression based method, which requires the System.Text.RegularExpressions namespace. The methods have the same result. You may want to place them in a separate class.
First method: The first method uses IndexOf. It finds all the newline characters.
Second method: The second method uses MatchCollection. This method uses the Matches method with RegexOptions.Multiline.IndexOf
C# program that counts lines in string
static void Main()
long a = CountLinesInString("This is an\r\nawesome website.");
long b = CountLinesInStringSlow("This is an awesome\r\nwebsite.\r\nYeah.");
static long CountLinesInString(string s)
long count = 1;
int start = 0;
while ((start = s.IndexOf('\n', start)) != -1)
static long CountLinesInStringSlow(string s)
Regex r = new Regex("\n", RegexOptions.Multiline);
MatchCollection mc = r.Matches(s);
return mc.Count + 1;
Here we compare the performance of these methods. I benchmarked the above methods for one million operations on real-world files to see just how different they perform. The timing results were more than one order of magnitude different.
So: Generally there is no advantage to the regular expression library when doing simple operations such as this.
Benchmark results for line counting:
Version A - Count: 188 ms [faster]
Version B - Regex: 5959 ms
Summary. We counted the number of lines in files by using StreamReader and ReadLine. We also looked at the ReadLine method and discussed other ways of counting lines in text data using the C# language.
© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to email@example.com.