---
title: "How do you read a file line by line using a generator-like approach (e.g., yield return)?"  
description: "How do you read a file line by line using a generator-like approach (e.g., yield return)?"  
author: "Ravi Vishwakarma"  
published: 2025-05-11  
updated: 2025-05-11  
canonical: https://www.mindstick.com/interview/34101/how-do-you-read-a-file-line-by-line-using-a-generator-like-approach-e-g-yield-return  
category: "c#"  
tags: ["c#", "file handling"]  
reading_time: 2 minutes  

---

# How do you read a file line by line using a generator-like approach (e.g., yield return)?

You can create a **generator-like method** using `yield return` to **read a file line by line**. This is memory-efficient, especially for large files, because it reads one line at a time and doesn’t load the whole file into memory.

### Example: Generator-style file reader

```cs
using System;
using System.Collections.Generic;
using System.IO;

class Program
{
    static void Main()
    {
        string path = @"C:\example.txt";

        foreach (var line in ReadLines(path))
        {
            Console.WriteLine(line);
        }
    }

    static IEnumerable<string> ReadLines(string filePath)
    {
        using (var reader = new StreamReader(filePath))
        {
            string? line;
            while ((line = reader.ReadLine()) != null)
            {
                yield return line;
            }
        }
    }
}
```

### Why use `yield return` here?

1. **Defers execution:** lines are read **only when needed**.
2. **Saves memory:** no need to store all lines in a list or array.
3. Makes the code composable with LINQ or `foreach`.

> You can combine this with LINQ filters like:

```cs
var longLines = ReadLines("file.txt").Where(l => l.Length > 80);
```

## Answers

### Answer by Ravi Vishwakarma

You can create a **generator-like method** using `yield return` to **read a file line by line**. This is memory-efficient, especially for large files, because it reads one line at a time and doesn’t load the whole file into memory.

### Example: Generator-style file reader

```cs
using System;
using System.Collections.Generic;
using System.IO;

class Program
{
    static void Main()
    {
        string path = @"C:\example.txt";

        foreach (var line in ReadLines(path))
        {
            Console.WriteLine(line);
        }
    }

    static IEnumerable<string> ReadLines(string filePath)
    {
        using (var reader = new StreamReader(filePath))
        {
            string? line;
            while ((line = reader.ReadLine()) != null)
            {
                yield return line;
            }
        }
    }
}
```

### Why use `yield return` here?

1. **Defers execution:** lines are read **only when needed**.
2. **Saves memory:** no need to store all lines in a list or array.
3. Makes the code composable with LINQ or `foreach`.

> You can combine this with LINQ filters like:

```cs
var longLines = ReadLines("file.txt").Where(l => l.Length > 80);
```


---

Original Source: https://www.mindstick.com/interview/34101/how-do-you-read-a-file-line-by-line-using-a-generator-like-approach-e-g-yield-return

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
