Xdiff 1.0.2

There is a newer version of this package available.
See the version list below for details.
dotnet add package Xdiff --version 1.0.2
                    
NuGet\Install-Package Xdiff -Version 1.0.2
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Xdiff" Version="1.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Xdiff" Version="1.0.2" />
                    
Directory.Packages.props
<PackageReference Include="Xdiff" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Xdiff --version 1.0.2
                    
#r "nuget: Xdiff, 1.0.2"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Xdiff@1.0.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Xdiff&version=1.0.2
                    
Install as a Cake Addin
#tool nuget:?package=Xdiff&version=1.0.2
                    
Install as a Cake Tool

Xdiff

A managed C# port of the xdiff library, providing structured diffs, unified diff text, and three-way merges. Supports Myers, patience, and histogram diff algorithms.

See Compatibility and reference testing for buffer-level scope, output framing, and reference-fixture evidence.

Installation

dotnet add package Xdiff

Quick start

using Xdiff;

string patch = Diff.UnifiedDiff("hello\nworld\n", "hello\nXdiff\n");
Console.WriteLine(patch);

Configure a unified diff

Choose an algorithm and control how many unchanged lines surround each change.

using Xdiff;

string patch = Diff.UnifiedDiff(
    "apple\nbanana\ncherry\n",
    "apple\nblueberry\ncherry\n",
    new DiffOptions
    {
        Algorithm = DiffAlgorithm.Histogram,
        ContextLines = 1,
        IndentHeuristic = true,
    });

Console.Write(patch);

Output contains hunk headers and changed lines; file headers (--- and +++) are not added.

@@ -1,3 +1,3 @@
 apple
-banana
+blueberry
 cherry

Ignore whitespace changes

Use WhitespaceMode.IgnoreAll to compare lines without considering whitespace.

using Xdiff;

string patch = Diff.UnifiedDiff(
    "int count = 1;\n",
    "int  count=1;   \n",
    new DiffOptions { Whitespace = WhitespaceMode.IgnoreAll });

Console.WriteLine(patch.Length == 0); // True

Other modes ignore changes in whitespace amounts, trailing whitespace, or CRLF versus LF line endings. IgnoreBlankLines can suppress changes consisting only of blank lines.

Inspect structured hunks and lines

Diff.Compute accepts byte buffers and returns line ranges and individual changes.

using System.Text;
using Xdiff;

byte[] oldData = Encoding.UTF8.GetBytes("hello\nworld\n");
byte[] newData = Encoding.UTF8.GetBytes("hello\nXdiff\n");
DiffResult result = Diff.Compute(oldData, newData);

foreach (DiffHunk hunk in result.Hunks)
{
    Console.WriteLine($"Old: {hunk.OldStart},{hunk.OldCount}; New: {hunk.NewStart},{hunk.NewCount}");

    foreach (DiffLine line in hunk.Lines)
    {
        string text = Encoding.UTF8.GetString(line.Content.Span).TrimEnd('\r', '\n');
        Console.WriteLine($"{line.Kind}: old={line.OldLine}, new={line.NewLine}: {text}");
    }
}

Line numbers are 1-based; OldLine is 0 for additions and NewLine is 0 for deletions. result.IsEmpty indicates that no hunks were emitted. Line content borrows from the input buffers, so do not modify those buffers while consuming the result.

Merge independent edits

Pass the common ancestor first, followed by each edited version. The string overload returns the merged text.

using Xdiff;

string merged = Merger.Merge(
    ancestor: "title\nbody\nfooter\n",
    ours:     "new title\nbody\nfooter\n",
    theirs:   "title\nbody\nnew footer\n");

Console.Write(merged); // "new title\nbody\nnew footer\n"

Detect and display merge conflicts

Use the byte overload to get HasConflicts and ConflictCount. MergeStyle.Diff3 includes the ancestor in conflict markers, and labels identify each version.

using System.Text;
using Xdiff;

MergeResult result = Merger.Merge(
    Encoding.UTF8.GetBytes("color=blue\n"),
    Encoding.UTF8.GetBytes("color=red\n"),
    Encoding.UTF8.GetBytes("color=green\n"),
    new MergeOptions
    {
        Style = MergeStyle.Diff3,
        AncestorLabel = "base",
        OurLabel = "ours",
        TheirLabel = "theirs",
    });

if (result.HasConflicts)
{
    Console.WriteLine($"Unresolved conflicts: {result.ConflictCount}");
}

Console.Write(Encoding.UTF8.GetString(result.Content));
Unresolved conflicts: 1
<<<<<<< ours
color=red
||||||| base
color=blue
=======
color=green
>>>>>>> theirs

To resolve conflicting regions automatically, set Favor = MergeFavor.Ours, MergeFavor.Theirs, or MergeFavor.Union in MergeOptions. These choices preserve non-conflicting edits from both sides; Union includes both sides of a conflict. The string overload can also emit conflict markers, but does not return a conflict count.

See the documentation for more examples and the full options reference.

License and attribution

The C# port is copyright (C) 2026 Yi Jin and licensed under LGPL-2.1-or-later, with applicable upstream terms. The histogram implementation includes code under the Eclipse Distribution License v1.0 (SPDX: BSD-3-Clause). The PooledByteBufferWriter helper is licensed under MIT.

The package includes LICENSE and THIRD-PARTY-NOTICES.md. See the license and third-party notices for the license text, upstream attribution, and disclaimers.

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net10.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.1.1 85 9/18/2026
1.0.2 90 9/17/2026