ComparableLibrary 2.0.0

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

ComparableLibrary

This solution was created to demonstrate how to compare two objects by value. For this, the IGeneralComparable interface was created, which provides a way to generate a hash from significant fields, allowing objects to be compared using this hash.

The project was implemented in C# using the System.Data.HashFunction.MurmurHash NuGet package for hashing with the MurmurHash3 algorithm.

Core Idea

  • Use the existing IGeneralComparable marker interface (already in this repo).
  • Annotate only those properties that should affect comparison with ComparablePropertyAttribute.
  • Optionally inherit from GeneralComparable if you want the HashSum convenience property; otherwise call the extension directly.
  • Compute the hash with the provided extension: GeneralComparableExtensions.GetHashSum(this IGeneralComparable).

API in this repo

  • IGeneralComparable — marker interface.
  • ComparablePropertyAttribute (properties: Name, Order, Type).
  • GeneralComparable — base class that caches a computed HashSum.
  • GeneralComparableExtensions.GetHashSum(IGeneralComparable) — extension method that:
    • walks public instance properties with [ComparableProperty],
    • normalizes primitives/strings/decimals (culture‑invariant),
    • supports bool, floating‑point "R" round‑trip formatting,
    • understands collections via ComparableCollectionType (Ordered/Unordered),
    • folds nested IGeneralComparable items by their own hash,
    • returns a stable MurmurHash3 hex string.

Usage Examples (taken from tests in this repo)

1) Simple comparable (uses your SampleComparable)

using ComparableLibrary;
using ComparableLibrary.Utils;

public class SampleComparable : GeneralComparable
{
    [ComparableProperty(2)]
    public string TextProperty { get; set; }

    [ComparableProperty(1)]
    public int NumberProperty { get; set; }

    [ComparableProperty(4)]
    public decimal Price { get; set; }

    [ComparableProperty(3)]
    public DateTime CreatedAt { get; set; }
}

var a = new SampleComparable { TextProperty = "A", NumberProperty = 10, Price = 12.34m, CreatedAt = DateTime.UtcNow };
var b = new SampleComparable { TextProperty = "A", NumberProperty = 10, Price = 12.34m, CreatedAt = a.CreatedAt };

// Either use the base class property:
string hashA = a.HashSum;

// Or the extension method:
string hashB = b.GetHashSum();

bool equalByValue = hashA == hashB; // true when significant properties are equal

2) Cross‑type mapping by Name (uses your NameMappedAA and NameMappedBB)

public class NameMappedAA : IGeneralComparable
{
    [ComparableProperty(Order = 0, Name = "Key")]
    public string KeyA { get; set; }
}

public class NameMappedBB : IGeneralComparable
{
    [ComparableProperty(Order = 0, Name = "Key")]
    public string KeyB { get; set; }
}

// Instances with the same logical "Key" produce identical hashes
var x = new NameMappedAA { KeyA = "42" };
var y = new NameMappedBB { KeyB = "42" };
bool same = x.GetHashSum() == y.GetHashSum(); // true

3) Collections (ordered vs unordered)

public class Basket : IGeneralComparable
{
    // Order matters (sequence is part of the hash)
    [ComparableProperty(Order = 1, Type = ComparableCollectionType.Ordered)]
    public List<string> Lines { get; set; } = new();

    // Order does not matter (set semantics)
    [ComparableProperty(Order = 2, Type = ComparableCollectionType.Unordered)]
    public HashSet<int> Tags { get; set; } = new();
}

Behavior Notes

  • null vs empty: encoded distinctly in the hash.
  • Strings/numbers/dates: normalized using invariant culture; float/double use round‑trip ("R") formatting.
  • Collections: choose Type to reflect semantics; unordered folding is position‑independent.
  • Nested models: if a property type implements IGeneralComparable, its GetHashSum() is folded into the parent hash.

Users can view examples of library usage in ComparableLibraryTest

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.

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
2.0.1 66 8/24/2026
2.0.0 68 8/24/2026
1.1.1 192 10/8/2025
1.1.0 182 10/8/2025
1.0.0 197 7/7/2025