Lazily reading a file line-by-line via an IEnumerable

suggest change

When working with large files, you can use the System.IO.File.ReadLines method to read all lines from a file into an IEnumerable<string>. 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 ToArray, 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.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


File and Stream IO:
* Lazily reading a file line-by-line via an IEnumerable

Table Of Contents
17 Regex
19 Arrays
21 Enum
22 Tuples
24 GUID
27 Looping
36 Casting
46 Methods
88 Events
92 Structs
95 File and Stream IO
104 Indexer
106 Stream
107 Timers
109 Threading
127 Caching
135 Pointers
147 C# Script