ModernBCL.Core.HashCode
1.0.0
Migrate to ModernBCL.Core.BLC, improve performance and implement more features.
dotnet add package ModernBCL.Core.HashCode --version 1.0.0
NuGet\Install-Package ModernBCL.Core.HashCode -Version 1.0.0
<PackageReference Include="ModernBCL.Core.HashCode" Version="1.0.0" />
<PackageVersion Include="ModernBCL.Core.HashCode" Version="1.0.0" />
<PackageReference Include="ModernBCL.Core.HashCode" />
paket add ModernBCL.Core.HashCode --version 1.0.0
#r "nuget: ModernBCL.Core.HashCode, 1.0.0"
#:package ModernBCL.Core.HashCode@1.0.0
#addin nuget:?package=ModernBCL.Core.HashCode&version=1.0.0
#tool nuget:?package=ModernBCL.Core.HashCode&version=1.0.0
ModernBCL.Core.HashCode
A high-performance polyfill for the System.HashCode struct, backported to .NET Framework 4.8. This library enables developers to efficiently implement the required GetHashCode() method for custom types used in Dictionary and other hash-based collections, overcoming a key feature gap in the legacy BCL.
🚀 Motivation
The modern System.HashCode struct (introduced in .NET Core) provides a fast, non-allocating, and, most importantly, order-sensitive way to combine multiple fields into a single hash code. Without it, developers on .NET 4.8 must write complex, manual bit-mixing code, which is often error-prone and leads to hash collisions.
This package provides a drop-in solution to ensure your custom dictionary keys maintain high performance and correctness in legacy applications.
✨ Features
Order Sensitivity: Generates a different hash for (A, B) versus (B, A).
Performance: Uses optimized bitwise operations (rotations, XORs) for fast calculation.
Non-Allocating: Designed as a struct to avoid heap allocations.
Simple API: Provides clean static Combine helpers up to 8 arguments.
📦 Installation
Install the package via NuGet:
.NET CLI
dotnet add package ModernBCL.Core.HashCode
Package Manager Console
Install-Package ModernBCL.Core.HashCode
💻 Usage
Once installed, use the static HashCode.Combine method inside your custom class's GetHashCode() override.
Example: ProductKey.cs
using System; // Note: HashCode is available automatically in the System namespace
public class ProductKey { public string ProductId { get; } public string Color { get; } public int Size { get; } // ... constructor and Equals implementation omitted for brevity
public override int GetHashCode()
{
// One clean line replaces dozens of manual mixing operations.
return HashCode.Combine(ProductId, Color, Size);
}
}
🤝 Contribution
Contributions are welcome! If you find a bug or want to suggest a performance improvement for the underlying hashing algorithm, please open an issue or submit a pull request on GitHub.
📝 License
This project is licensed under the MIT License - see the LICENSE file for details.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET Framework | net481 is compatible. |
-
.NETFramework 4.8.1
- 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 |
|---|