Lazily reading a file line-by-line via an IEnumerable
When working with large files, you can use the
System.IO.File.ReadLines method to read all lines from a file into an
IEnumerable. This is similar to
System.IO.File.ReadAllLines, except that it doesn’t load the whole file into memory at once, making it more efficient when working with large files.
IEnumerable<string> AllLines = File.ReadLines("file_name.txt", Encoding.Default);
The second parameter of File.ReadLines is optional. You may use it when it is required to specify encoding.
It is important to note that calling
ToList or another similar function will force all of the lines to be loaded at once, meaning that the benefit of using
ReadLines is nullified. It is best to enumerate over the
IEnumerable using a
foreach loop or LINQ if using this method.