Philiprehberger.TextDiff 0.1.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Philiprehberger.TextDiff --version 0.1.0
                    
NuGet\Install-Package Philiprehberger.TextDiff -Version 0.1.0
                    
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="Philiprehberger.TextDiff" Version="0.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Philiprehberger.TextDiff" Version="0.1.0" />
                    
Directory.Packages.props
<PackageReference Include="Philiprehberger.TextDiff" />
                    
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 Philiprehberger.TextDiff --version 0.1.0
                    
#r "nuget: Philiprehberger.TextDiff, 0.1.0"
                    
#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 Philiprehberger.TextDiff@0.1.0
                    
#: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=Philiprehberger.TextDiff&version=0.1.0
                    
Install as a Cake Addin
#tool nuget:?package=Philiprehberger.TextDiff&version=0.1.0
                    
Install as a Cake Tool

Philiprehberger.TextDiff

CI NuGet License

Line-by-line text diff using Myers' algorithm — unified diff output, side-by-side comparison, and structured DiffResult objects.

Installation

dotnet add package Philiprehberger.TextDiff

Usage

using Philiprehberger.TextDiff;

var result = Diff.Compare("hello\nworld", "hello\nearth");
Console.WriteLine($"Changes: {result.HasChanges}, Added: {result.AddedCount}, Removed: {result.RemovedCount}");

Compare Strings

using Philiprehberger.TextDiff;

var result = Diff.Compare("line one\nline two\nline three", "line one\nline 2\nline three");

foreach (var line in result.Lines)
{
    var prefix = line.Type switch
    {
        DiffLineType.Added => "+",
        DiffLineType.Removed => "-",
        _ => " "
    };
    Console.WriteLine($"{prefix} {line.Content}");
}

Unified Diff Output

using Philiprehberger.TextDiff;

var oldText = "alpha\nbeta\ngamma\ndelta";
var newText = "alpha\nbeta\nepsilon\ndelta";

var unified = Diff.Unified(oldText, newText, "file-v1.txt", "file-v2.txt", context: 2);
Console.WriteLine(unified);
// --- file-v1.txt
// +++ file-v2.txt
// @@ -1,4 +1,4 @@
//  alpha
//  beta
// -gamma
// +epsilon
//  delta

Structured DiffResult

using Philiprehberger.TextDiff;

var result = Diff.Compare("a\nb\nc", "a\nc");

Console.WriteLine($"Has changes: {result.HasChanges}");
Console.WriteLine($"Added: {result.AddedCount}");
Console.WriteLine($"Removed: {result.RemovedCount}");
Console.WriteLine($"Unchanged: {result.UnchangedCount}");

API

Diff

Method Description
Compare(oldText, newText) Compares two texts and returns a DiffResult with lines and statistics
Unified(oldText, newText, oldLabel?, newLabel?, context?) Produces a unified diff string with ---, +++, and @@ headers
Lines(oldText, newText) Returns a list of DiffLine entries with change types and line numbers

DiffResult

Property Type Description
Lines IReadOnlyList<DiffLine> The individual diff lines
AddedCount int Number of added lines
RemovedCount int Number of removed lines
UnchangedCount int Number of unchanged lines
HasChanges bool Whether the texts differ

DiffLine

Property Type Description
Type DiffLineType Added, Removed, or Unchanged
Content string The text content of the line
OldLineNumber int? 1-based line number in the old text
NewLineNumber int? 1-based line number in the new text

Development

dotnet build src/Philiprehberger.TextDiff.csproj --configuration Release

License

MIT

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  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.
  • net8.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
0.2.1 129 4/1/2026
0.2.0 122 3/28/2026
0.1.1 111 3/23/2026
0.1.0 111 3/21/2026