Lazily reading a ﬁle line-by-line via an C# with Example
When working with large ﬁles, you can use the System.IO.File.ReadLines method to read all lines from a ﬁle into
an IEnumerable. This is similar to System.IO.File.ReadAllLines, except that it doesn't load the whole ﬁle
into memory at once, making it more eﬃcient when working with large ﬁles.
IEnumerable 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 ToArray, ToList or another similar function will force all of the lines to be loaded
at once, meaning that the beneﬁt of using ReadLines is nulliﬁed. It is best to enumerate over the IEnumerable using
a foreach loop or LINQ if using this method.