I need a HashSet that preserves insertion ordering, are there any implementations of this in the framework?
I need a HashSet that preserves insertion ordering, are there any implementations of this in the framework?
If you need constant complexity of Add, Remove, Contains and order preservation, then there's no such collection in .NET Framework 4.5.
If you're okay with 3rd party code, take a look at my repository (permissive MIT license): https://github.com/OndrejPetrzilka/Rock.Collections
There's OrderedHashSet<T> collection:
HashSet<T> source code (from .NET Core)HashSet<T>Add and Remove operations are 20% slower compared to HashSet<T>You can use OrderedDictionary to preserve the order of insertion. But beware of the cost of Removing items (O(n)).