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.
- Host: GitHub
- URL: https://github.com/andeart/unitylabs.enumcomparer.3.5
- Owner: andeart
- License: mit
- Created: 2019-01-23T05:48:25.000Z (almost 7 years ago)
- Default Branch: main
- Last Pushed: 2024-08-26T19:51:36.000Z (over 1 year ago)
- Last Synced: 2025-01-03T01:54:48.226Z (about 1 year ago)
- Topics: csharp, dictionary, dotnet, enums, hashset
- Language: C#
- Size: 2.93 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
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...