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
Release: 2025-01-05 01:25:40
Original
381 people have browsed it

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;
        }
    }
}
Copy after login

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;
        }
    }
}
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template