---
title: "Use patience diff for source code in C#"
url: https://recursiveai.org/onediff/cookbook/diff-lines-with-patience/
description: "Compare source code line by line in C# with OneDiff’s patience algorithm. See how the diff displays reordered methods and repeated braces."
version: "1.0.1"
---

# Use patience diff for source code 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/diff-lines-with-patience/diff-lines-with-patience.zip)

Patience diff uses distinctive lines as anchors when comparing two versions of source code.

```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;
string first = "void A()\n{\n    RunA();\n}\n";
string second = "void B()\n{\n    RunB();\n}\n";
var diff = Diff.Lines(first + second,
    second + first, DiffAlgorithm.Patience);
Console.Write(Diff.ToTerminal(diff, false));
```

Unzip [the example](https://recursiveai.org/onediff/cookbook/diff-lines-with-patience/diff-lines-with-patience.zip), then `dotnet run`.

Prints:

```text
@@ -1,8 +1,8 @@
-void A()
-{
-    RunA();
-}
 void B()
 {
     RunB();
+}
+void A()
+{
+    RunA();
 }
```

The example swaps two methods. The edit script shows removals and additions, not a “move” event. The last closing brace is unchanged because the two strings share that line at the end; a line diff does not know which method owns a brace.

## Variations

**Compare two files.** Replace the input strings and the `Diff.Lines` call with the following. Pass the paths in order: `dotnet run -- before.cs after.cs`. Keep both files outside the console project directory so they are read as text rather than compiled into the example.

```csharp
var diff = Diff.Lines(File.ReadAllText(args[0]),
    File.ReadAllText(args[1]), DiffAlgorithm.Patience);
```

**Show only the kinds of edits.** Replace the final `Console.Write` with this loop to inspect the underlying script instead of the formatted patch:

```csharp
foreach (var edit in diff.Script.Edits)
    Console.WriteLine(edit.Kind);
```

## Related recipes

- [Compare two strings word by word](https://recursiveai.org/onediff/cookbook/compare-text/)
- [Build a side-by-side HTML diff](https://recursiveai.org/onediff/cookbook/side-by-side-html-diff/)

## The short answer

Call `Diff.Lines(before, after, DiffAlgorithm.Patience)` with both source versions as strings. `Diff.ToTerminal(diff, color: false)` gives you a readable line diff without color codes. Use the edit script instead if your application already has its own way to render additions and deletions. Try patience when the default diff matches repeated lines across reordered methods, making a move look like several unrelated edits.

## How does patience diff compare source code lines?

After trimming identical lines from the start and end of the range being compared, OneDiff finds lines that appear exactly once in each remaining range. It picks the longest set of those lines that appear in the same order in both versions. Those anchors split the diff into smaller ranges, where it repeats the process. When there’s no unique matching line in a range, it uses Myers there, the standard minimal-edit algorithm and OneDiff’s default for line diffs. The [algorithm notes](https://recursiveai.org/onediff/docs/diff-algorithms/#patience) spell out the steps.

A line that only contains a brace can occur many times, so it often can’t serve as an anchor in the original range. A distinct method declaration can. A repeated line can still match, for example if it becomes unique between two anchors. Patience can use more edits to keep distinctive lines together. Myers aims for the fewest inserted and deleted lines, unless it reaches its work cap.


For a small example where the choice matters, swap two methods that both call `Start()` and `Finish()`. The default diff lines up the repeated calls and shows both method names and their distinct calls as replacements. Patience instead keeps `void B()` and `RunB()` in the unchanged context, making the reorder easier to see. It still matches the identical `Finish();` and closing brace at the end of the file, so the diff doesn’t show a perfectly intact block moving.

```csharp
using OneDiff;

string a = "void A()\n{\n    Start();\n"
    + "    RunA();\n    Finish();\n}\n";
string b = "void B()\n{\n    Start();\n"
    + "    RunB();\n    Finish();\n}\n";
string before = a + b, after = b + a;

Console.WriteLine("Default:");
Console.Write(Diff.ToTerminal(Diff.Lines(before, after), false));
Console.WriteLine("Patience:");
Console.Write(Diff.ToTerminal(
    Diff.Lines(before, after, DiffAlgorithm.Patience), false));
```

```text
Default:
@@ -1,12 +1,12 @@
-void A()
+void B()
 {
     Start();
-    RunA();
+    RunB();
     Finish();
 }
-void B()
+void A()
 {
     Start();
-    RunB();
+    RunA();
     Finish();
 }
Patience:
@@ -1,12 +1,12 @@
-void A()
-{
-    Start();
-    RunA();
-    Finish();
-}
 void B()
 {
     Start();
     RunB();
+    Finish();
+}
+void A()
+{
+    Start();
+    RunA();
     Finish();
 }
```

## Does patience diff detect moved code blocks?

It can make a reordering easier to read, but the output does not identify moves as a separate kind of change. In the example, `void B()` remains in the shared context; the old `void A()` lines are removed and the new ones added in their new place. When the order changes, not every declaration can be an anchor, because the anchor sequence has to keep the same order on both sides.

Treat the output as a comparison of lines, not of C# declarations. A line may stay in the context even if a different method now uses it. Line endings are part of the comparison too, so if only the line endings changed, an otherwise identical line can still show as changed. Use the same line endings in both inputs if your review is only about code changes.
