Keep matching lines aligned when a line is inserted or deleted.
dotnet add package RecursiveAI.OneDiffRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using OneDiff; var diff = Diff.SideBySide( "Start\nEnd\n", "Start\nNew\nEnd\n"); // Row kind (discarded), old cell, new cell. foreach (var (_, left, right) in diff.Rows) Console.WriteLine( $"{left?.Text} | {right?.Text}");
Unzip the example, then dotnet run.
Prints the old line on the left and the new line on the right:
Start | Start | New End | End
The full program
The zip includes these two text files.
before.txt:
Mode: test Legacy Keep
after.txt:
Mode: live Keep Ready
The program prints each row’s kind and line numbers, then shows the word parts for the modified row. A dash stands in for a missing cell.
using OneDiff; using OneDiff.Output; string before = File.ReadAllText("before.txt"); string after = File.ReadAllText("after.txt"); var view = Diff.SideBySide(before, after); foreach (var row in view.Rows) Console.WriteLine($"{row.Kind,-9} {Show(row.Left)} | {Show(row.Right)}"); var changed = view.Rows.First(row => row.Kind == SideBySideKind.Modified); Console.WriteLine("Changed word parts:"); foreach (var part in changed.Left!.Parts) Console.WriteLine($"Left {part.Kind}: [{part.Text}]"); foreach (var part in changed.Right!.Parts) Console.WriteLine($"Right {part.Kind}: [{part.Text}]"); static string Show(SideBySideCell? cell) => cell is null ? "—" : $"{cell.Line}: {cell.Text}";
Modified 1: Mode: test | 1: Mode: live Deleted 2: Legacy | — Unchanged 3: Keep | 2: Keep Inserted — | 3: Ready Changed word parts: Left Equal: [Mode: ] Left Delete: [test] Right Equal: [Mode: ] Right Insert: [live]
If you build the rows from an existing TextDiff, use line tokens: SideBySideDiff.From rejects word and character diffs.
Variations
Reuse a line diff you already computed. Choose the line diff algorithm before building the rows:
using OneDiff; using OneDiff.Output; var lines = Diff.Lines("a\nb\n", "a\nc\n", DiffAlgorithm.Patience); var view = SideBySideDiff.From(lines); foreach (var row in view.Rows) Console.WriteLine(row.Kind);
Unchanged Modified
Related recipes
The short answer
Call Diff.SideBySide(oldText, newText).Rows for data you can bind to a two-column view. Each row gives you a kind (Unchanged, Modified, Inserted, or Deleted) and the old and new cells. For a cell that exists, Line is the one-based number in its original text, Text is the line without its line break, and Parts tells you which spans to highlight. The result is data, not HTML.
How do I compare two text files side by side?
If your inputs are files, read each as text and pass the two strings to Diff.SideBySide. Don’t pair lines just by their number: adding a line near the start of the new file would make every later pair look different. Instead, draw one table or list row per aligned row and take the numbers from the two cells. In the example, old line 3 and new line 2 both contain Keep, so they share a row even though their numbers differ.
Inside a block of changed lines, OneDiff pairs deletions and insertions in order. A pair becomes Modified; unpaired lines become Deleted or Inserted. That pairing describes a position in the changed block, not a guarantee that two lines have the same meaning. For example, replacing three lines with one gives one modified row and two deleted rows in that block. Use the row kind to choose the background, and leave a blank placeholder where the cell is null. Don’t use Text == "" to decide that a cell is missing: a real empty line still has a line number.
On a modified row, the left cell’s Parts contain Equal and Delete runs. The right cell’s contain Equal and Insert runs. Render the equal text normally and style only the deleted or inserted spans. Concatenating the parts yields the cell’s Text, so don’t add spaces between runs or trim them. If you render a browser UI yourself, insert the parts as text rather than treating input strings as markup.
If you only need to know whether two files are identical, a content equality check answers that without building rows. Use aligned rows when someone needs to inspect exactly where the lines diverged and where they come back into alignment. For output you can apply to another file, use a unified patch instead: the row text omits its line breaks and is meant for display.