Efficient Line-by-Line File Reading in C# Using LINQ
When processing text files where each line requires individual handling, StreamReader offers a straightforward solution. However, for improved efficiency and readability, LINQ provides a more elegant approach.
To avoid loading the entire file into memory, consider using an iterator block:
static IEnumerable<SomeType> ReadFrom(string file) { string line; using(var reader = File.OpenText(file)) { while((line = reader.ReadLine()) != null) { SomeType newRecord = /* parse line */ yield return newRecord; } } }
Alternatively, for a simpler version:
static IEnumerable<string> ReadFrom(string file) { string line; using(var reader = File.OpenText(file)) { while((line = reader.ReadLine()) != null) { yield return line; } } }
The ReadFrom(...) function returns a lazily evaluated sequence, avoiding data buffering. By combining ReadFrom(...) with other LINQ operations (e.g., Where, Select), efficient line-by-line processing can be achieved without sacrificing operational efficiency.
The above is the detailed content of How Can LINQ Improve Efficiency When Reading Files Line by Line in C#?. For more information, please follow other related articles on the PHP Chinese website!