An open API service indexing awesome lists of open source software.

https://github.com/andeart/unitylabs.enumcomparer.3.5

An optimised type-safe enum comparer that doesn't box enum values, created specifically for .NET Framework 3.5 and older.
https://github.com/andeart/unitylabs.enumcomparer.3.5

csharp dictionary dotnet enums hashset

Last synced: 11 months ago
JSON representation

An optimised type-safe enum comparer that doesn't box enum values, created specifically for .NET Framework 3.5 and older.

Awesome Lists containing this project

README

          

# UnityLabs.EnumComparer

An optimised type-safe enum comparer that doesn't box enum values, created specifically for .NET Framework 3.5 and older.
Newer .NET Framework versions fix the enum-value boxing, so this is not relevant in those environments.
This is a hybrid of:
1) Tyler Brinkley's Enums.NET at https://github.com/TylerBrinkley/Enums.NET (Copyright (c) 2016 Tyler Brinkley). Go give this beautiful repo a star.
2) .NET Framework 3.5's EqualityComparer code, found by peeking at the source (https://referencesource.microsoft.com/ now only shows the latest .NET Framework version, which implicitly fixes this very problem).

This is best used as a comparer in `Dictionary` and `Hashset` objects, as they both resort to old-school `ObjectEqualityComparer` when created via their default constructors (via `EqualityComparer.Default`).
Both these types do a comparison check each time you deserialize, lookup, insert, or remove an entry. `EnumComparer` saves boxing allocations in these cases.

"Don't be an unwitting boxer." - Wise person.

## Usage

* Drop `EnumComparer.cs` in your project.
* In your IDE, when creating a `Dictionary` or a `Hashset`, pass in `EnumComparer.Default` as the constructor argument.
* If you use Intellisense, it should automatically fill out the correct argument as you start typing `Default` as the constructor argument.

## Benchmark Results

Coming soon...