How to get Line Count of a File using StreamReader in C#

1.       How to get the Count of line Numbers in a file using StreamReader in C#
or
2.       How to get Line Count of a File using StreamReader in C#

If you happen to use StreamReader in C# to read text files and manipulate them, you might in some cases want to know the number of lines the text file has. I had a point where I had to know the line count of the file and I came up with the following different ways of getting the line count of a text file while using the StreamReader class in C#
Method 1:
In the first method you can read the text file and add it to a List Collection, one line at a time, till you reach the end of fine. And then use the Count() method of the List Collection to get the line count of the file. Refer to the code example below.
 
This might not be the most efficient way of getting the line count in a file, but it is one of the  ways  to get the line count. 
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
      
                List<string> records = new List<string>();
                using (StreamReader r = new StreamReader(inputFileName))
                {
                    string record;
                    while ((record = r.ReadLine()) != null)
                    {
                        records.Add(record);
                    }
                }
 
 
                totalRecordCount = records.Count();
  
Method 2:
The Second Method to get the Record Count is without adding the file line by line into a List Collection which can be very inefficient. Just read through the text file line by line till the Stream Reader reaches the end of file and use a counter variable to count the number of lines as shown in the following example. This code is more efficient than the one shown in Method 1.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
String totalRecordCount;
using (StreamReader r = new StreamReader(inputFileName))
                {
                    string record;
                    while ((record = r.ReadLine()) != null)
                    {
                        totalRecordCount++;
                    }
    }
 Console.WriteLine(totalRecordCount);
 
Method 3:
Finally, the third method is by using the Regular Expression.   In this method you read the entire text file using the StreamReader class and then use the Regular Expression (Regex Class) to look for new line character using the Matches method. Regex should come up with a collection of the list of matches. Then you can get the count of the collection which will be the number of lines in the text file. Here is a code example of how you do it.
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
 
string inputFileName = "InputFile.txt";
            string text;
 
            using (StreamReader r = new StreamReader(inputFileName))
            {              
                text = r.ReadToEnd();                 
            }
 
           
            Regex RE = new Regex("\\n", RegexOptions.Multiline);
            MatchCollection theMatches = RE.Matches(text);
 
            Console.WriteLine(theMatches.Count.ToString());       
            Console.Read();