dotnet add package RecursiveAI.OneDiffRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using OneDiff; Item[] a = [new(1), new(2)]; Item[] b = [new(1), new(3)]; var byId = EqualityComparer<Item>.Create( (x, y) => x?.Id == y?.Id, x => x.Id); var edits = Diff.Sequences<Item>(a, b, comparer: byId); Console.WriteLine(edits.EditDistance); record Item(int Id);
Have a List<T>? Pass list.ToArray() for each input.
Prints the insertion/deletion distance:
2Unzip the example, then dotnet run.
The full program
The download aligns records by ID and prints the zero-based ranges of every edit. Tea and TEA have the same ID.
using OneDiff; Item[] before = [new(1, "Tea"), new(2, "Cake")]; Item[] after = [new(1, "TEA"), new(3, "Soup")]; var byId = EqualityComparer<Item>.Create( (a, b) => a?.Id == b?.Id, item => item.Id); var edits = Diff.Sequences<Item>(before, after, comparer: byId); foreach (var edit in edits) Console.WriteLine($"{edit.Kind}: old {edit.OldStart}+{edit.OldLength}, new {edit.NewStart}+{edit.NewLength}"); record Item(int Id, string Name);
Equal: old 0+1, new 0+1 Delete: old 1+1, new 1+0 Insert: old 2+0, new 1+1
An equal run means equal under your comparer, not necessarily identical in every property.
Variations
Compare every record property
Replace the comparison in the full program; keep its input arrays and output loop.
// Replace the comparer argument in the full program: var edits = Diff.Sequences<Item>(before, after, comparer: EqualityComparer<Item>.Default);
Delete: old 0+2, new 0+0 Insert: old 2+0, new 0+2
Related recipes
The short answer
Use Diff.Sequences<T> with an IEqualityComparer<T>. OneDiff compares the original ordered items through that comparer and returns an EditScript containing equal, delete and insert runs. It doesn’t need a text representation of each object. In this example, matching IDs count as equal even when the names differ.
The snippet at the top prints the insertion/deletion distance. The full program prints each run’s positions so you can connect the result back to your own records. The sequence API source documents that those indices refer to the original input spans.
How do I compare two lists of objects in C#?
First decide what “compare” means. An ordered diff asks which runs must be kept, removed or inserted to align the old sequence with the new one. It preserves item positions. That is different from asking whether both collections contain the same distinct keys without regard to order.
Pass arrays, or spans of your items, to Diff.Sequences<T>. For a List<T>, materializing each list with ToArray() gives inputs the span-based API accepts. Keep those arrays available while reading the script, since a run contains ranges, not copies of the records themselves.
The full example’s equal run covers the first item on each side. Its delete run covers the old second item, Cake. Its insert run covers the new second item, Soup. “TEA” stays aligned with “Tea” because the chosen equality rule ignores the name. This is an ID alignment, not a report of every changed field.
How do I compare two lists by a single property?
Supply a comparer whose equality and hash-code functions both use that property. Here both functions use Id. If you add another property to equality, update the hash function to remain consistent: equal objects must have equal hash codes. The comparison interns items through this comparer before running the diff algorithm.
The top snippet defines a small record with an ID only. The full program adds names to show why key equality matters. The variation removes that custom equality rule and uses the record’s default comparer instead. Then the changed capitalization matters too, so both original records appear in the deleted run and both new records appear in the inserted run.
Choosing a key is part of the result’s meaning. If two occurrences share a key, the algorithm can treat them as equal items even when their other properties differ. If you need a field-change report, align by key first, then compare the aligned records’ fields separately; an equal run alone doesn’t certify that every property matches.
How is a list diff different from C# Except?
A sequence diff reports an alignment, including equal runs and positions in both inputs. Set difference answers membership instead. Microsoft defines Except as producing the set difference of two sequences, returning unique elements. It isn’t a replacement for an ordered edit script when repeated occurrences and positions matter.
In an Edit, OldStart and NewStart are zero-based item indices. Lengths count items, not characters or bytes. A delete has an old length but no new length; an insert has a new length but no old length. A replacement appears as a deletion followed by an insertion, not a separate modified-item operation. Use these ranges to select records from the corresponding input rather than assuming each edit is a single item.