---
title: "What class would you use to read a file line-by-line?"  
description: "What class would you use to read a file line-by-line?"  
author: "ICSM Computer"  
published: 2025-05-06  
updated: 2025-05-20  
canonical: https://www.mindstick.com/forum/161579/what-class-would-you-use-to-read-a-file-line-by-line  
category: "c#"  
tags: ["c#", "file handling"]  
reading_time: 1 minute  

---

# What class would you use to read a file line-by-line?

What [class](https://www.mindstick.com/blog/165/generic-class-in-c-sharp) would you use to read a [file](https://www.mindstick.com/articles/59/encrypting-and-decrypting-files-using-c-sharp) line-by-line?

## Replies

### Reply by Utpal Vishwas

To read a file **line by line** in C#, you commonly use the `StreamReader` class.

## Best Class: `System.IO.StreamReader`

### Example:

```cs
using System;
using System.IO;

class Program
{
    static void Main()
    {
        using (StreamReader reader = new StreamReader("file.txt"))
        {
            string? line;
            while ((line = reader.ReadLine()) != null)
            {
                Console.WriteLine(line);
            }
        }
    }
}
```

1. `StreamReader.ReadLine()` reads one line at a time.
2. The `using` statement ensures the file is closed properly after reading.

## Alternative: `File.ReadLines()`

For simpler cases, you can use `File.ReadLines()`, which is more concise and memory-efficient for large files:

```cs
foreach (string line in File.ReadLines("file.txt"))
{
    Console.WriteLine(line);
}
```

Also reads lazily (line by line), unlike `File.ReadAllLines()` which reads the entire file at once.

### Summary

| Class/Method | Description | Reads Line-by-Line? |
| --- | --- | --- |
| `StreamReader` | Low-level, full control | Yes |
| `File.ReadLines()` | Recommended for simplicity | Yes |
| `File.ReadAllLines()` | Loads all lines into memory array | No (not lazy) |


---

Original Source: https://www.mindstick.com/forum/161579/what-class-would-you-use-to-read-a-file-line-by-line

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
