---
title: "Get changed line numbers between two text files in C#"
url: https://recursiveai.org/onediff/cookbook/list-changed-line-numbers-between-text-files/
description: "Compare two text files in C# and get separate 1-based lists: added line numbers in the new file and removed line numbers in the old file."
version: "1.0.1"
---

# Get changed line numbers between two files in C#

**OneDiff** is a free, open-source diff library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneDiff) · [GitHub](https://github.com/recursive-ai-oss/onediff-csharp) · [Docs](https://recursiveai.org/onediff/docs/) · [Download example (.zip)](https://recursiveai.org/onediff/cookbook/list-changed-line-numbers-between-text-files/list-changed-line-numbers-between-text-files.zip)

```shell
dotnet add package RecursiveAI.OneDiff
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
using OneDiff;
var lines = Diff.Lines(
    File.ReadAllText("before.txt"),
    File.ReadAllText("after.txt")).Lines;
var add = lines.Where(x => x.OldLine is null);
var del = lines.Where(x => x.NewLine is null);
Console.WriteLine("Added: " + string.Join(',',
    add.Select(x => x.NewLine)));
Console.WriteLine("Removed: " + string.Join(
    ',', del.Select(x => x.OldLine)));
```

Unzip [the example](https://recursiveai.org/onediff/cookbook/list-changed-line-numbers-between-text-files/list-changed-line-numbers-between-text-files.zip), then `dotnet run`.

Prints the added and removed line numbers:

```text
Added: 3
Removed: 2
```

Both input files are in the download. Here’s `before.txt`:

```text
Start
Draft
Review
End
```

And `after.txt`:

```text
Start
Review
Publish
End
```

`Review` moved up a row after `Draft` was deleted, but its text didn’t change, so it isn’t listed. Run from the unzipped project directory: a missing input file causes `File.ReadAllText` to throw `FileNotFoundException`.

## Variations

**Add the changed text to each annotation.** Replace the code after the `lines` declaration. Use the old line number for a deletion and the new line number for an insertion.

```csharp
foreach (var line in lines)
{
    if (line.Kind == EditKind.Delete)
        Console.WriteLine($"-{line.OldLine}: {line.Text}");
    if (line.Kind == EditKind.Insert)
        Console.WriteLine($"+{line.NewLine}: {line.Text}");
}
```

```text
-2: Draft
+3: Publish
```

## Related recipes

- [Build a side-by-side HTML diff](https://recursiveai.org/onediff/cookbook/side-by-side-html-diff/)
- [Export a unified diff patch](https://recursiveai.org/onediff/cookbook/export-unified-diff-patch/)

## The short answer

Read the files as text, call `Diff.Lines(before, after)`, then filter its `Lines` view. An insertion has no `OldLine`, so take `NewLine`; a deletion has no `NewLine`, so take `OldLine`. You can also filter by `EditKind.Insert` and `EditKind.Delete`, as the annotation example does. Both sets of numbers start at 1. Keep them separate: line 3 of the new file and line 3 of the old file need not describe the same text. The line number on the missing side is `null`, not zero.

## How do you find different lines in two files?

Compare the sequences of lines, not two arrays at matching indexes. In the example, the old line 2 is `Draft` and the new line 2 is `Review`. Comparing the files by index would flag that row, and it would flag row 3 too, where the old file has `Review` and the new one has `Publish`. The line diff instead matches the two occurrences of `Review` and identifies the actual deletion and insertion around it.

To annotate a review, attach added numbers to the new file and removed numbers to the old file. A replacement appears as a deletion plus an insertion in this view. Don’t pair the two lists by index: a block can have unequal numbers of removals and additions. If you want a visual alignment instead of annotations keyed to file lines, the [side-by-side view](https://recursiveai.org/onediff/docs/text-output/#reading-views) pairs deletions and insertions within a changed block.

The [line view](https://recursiveai.org/onediff/docs/text-output/#reading-views) drops line breaks from each returned `Text` value. It still counts an unterminated final line as a line. Start with `Diff.Lines`, not `Diff.Words`, for a line-number list: a word diff can return several entries with the same line number because it tracks edits within a line.
