Home >Backend Development >C++ >How Can LINQ Improve Efficiency When Reading Files Line by Line in C#?

How Can LINQ Improve Efficiency When Reading Files Line by Line in C#?

Linda Hamilton
Linda HamiltonOriginal
2025-01-05 01:25:40404browse

How Can LINQ Improve Efficiency When Reading Files Line by Line in C#?

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn