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
C# Program to count the number of lines in a file
Counting the number of lines in a file is a common task in C# programming. This can be accomplished using several methods from the System.IO namespace. The most straightforward approach is to use the File.ReadAllLines() method combined with the Length property.
Using File.ReadAllLines() Method
The File.ReadAllLines() method reads all lines from a file into a string array, and then we can use the Length property to count the total number of lines −
Example
using System;
using System.IO;
public class Program {
public static void Main() {
// First, create a file with some content
using (StreamWriter sw = new StreamWriter("hello.txt")) {
sw.WriteLine("This is demo line 1");
sw.WriteLine("This is demo line 2");
sw.WriteLine("This is demo line 3");
sw.WriteLine("This is demo line 4");
}
// Count the lines using ReadAllLines
int count = File.ReadAllLines("hello.txt").Length;
Console.WriteLine("Number of lines: " + count);
}
}
The output of the above code is −
Number of lines: 4
Using StreamReader for Large Files
For large files, reading all lines into memory at once might not be efficient. Using StreamReader allows us to read the file line by line without loading everything into memory −
Example
using System;
using System.IO;
public class Program {
public static void Main() {
// Create a larger sample file
using (StreamWriter sw = new StreamWriter("large_file.txt")) {
for (int i = 1; i <= 1000; i++) {
sw.WriteLine("Line number " + i);
}
}
// Count lines using StreamReader
int lineCount = 0;
using (StreamReader sr = new StreamReader("large_file.txt")) {
while (sr.ReadLine() != null) {
lineCount++;
}
}
Console.WriteLine("Total lines in file: " + lineCount);
}
}
The output of the above code is −
Total lines in file: 1000
Using File.ReadLines() Method
The File.ReadLines() method provides a memory-efficient way to count lines by returning an enumerable that reads the file lazily −
Example
using System;
using System.IO;
using System.Linq;
public class Program {
public static void Main() {
// Create a test file
using (StreamWriter sw = new StreamWriter("test.txt")) {
sw.WriteLine("First line");
sw.WriteLine("Second line");
sw.WriteLine("Third line");
sw.WriteLine("Fourth line");
sw.WriteLine("Fifth line");
}
// Count lines using File.ReadLines with LINQ
int count = File.ReadLines("test.txt").Count();
Console.WriteLine("Line count using ReadLines: " + count);
}
}
The output of the above code is −
Line count using ReadLines: 5
Comparison of Methods
| Method | Memory Usage | Best For |
|---|---|---|
| File.ReadAllLines().Length | High (loads entire file) | Small to medium files |
| StreamReader with loop | Low (reads line by line) | Large files |
| File.ReadLines().Count() | Low (lazy enumeration) | Any file size with LINQ support |
Conclusion
Counting lines in a file can be achieved through multiple approaches in C#. Use File.ReadAllLines().Length for small files, StreamReader for memory-efficient processing of large files, or File.ReadLines().Count() for a LINQ-based approach that combines efficiency with clean syntax.
