Spargine is a collection of open-source assemblies and NuGet packages designed for .NET 10, which I have been developing and maintaining since the release of .NET Framework 2. These assemblies are not only a core part of my projects but are also actively deployed in production environments across several companies I collaborate with.
Get Spargine
You can access the source code and NuGet packages here:
- GitHub: Spargine 10
- NuGet: dotNetDaveNuGet
The HashSet<T> collection in .NET is a fast and efficient way to store unique elements—but it’s not thread-safe. That can be a serious drawback in multi-threaded or parallel applications where concurrent access may lead to data corruption or runtime exceptions.
To address this, I created a thread-safe alternative: ConcurrentHashSet<T>, available in the DotNetTips.Spargine.Core project and NuGet package. This custom collection mirrors the API and behavior of HashSet<T>, while safely supporting concurrent operations across threads.
Key Features
- Fully thread-safe implementation
- Mirrors familiar
HashSet<T>semantics - Supports custom comparers
- Offers multiple constructors to control the concurrency level and capacity
Constructors
- ConcurrentHashSet()
Default constructor. - ConcurrentHashSet(IEqualityComparer<T> comparer)
Uses a custom comparer. - ConcurrentHashSet(IEnumerable<T> collection)
Initializes from an existing collection. - ConcurrentHashSet(IEnumerable<T> collection, IEqualityComparer<T> comparer)
Initializes from a collection with a custom comparer. - ConcurrentHashSet(int concurrencyLevel, int capacity)
Controls internal partitioning and initial capacity. - ConcurrentHashSet(int concurrencyLevel, IEnumerable<T> collection, IEqualityComparer<T> comparer)
Full control over initialization parameters. - ConcurrentHashSet(int concurrencyLevel, int capacity, IEqualityComparer<T> comparer)
Fine-tuned concurrency and equality logic.
Available Methods
- Add(T item)
Adds an item to the set if it doesn’t already exist. - AddRange(IEnumerable<T> items)
Adds multiple items to the ConcurrentHashSet. - Clear()
Removes all elements from the set. - Contains(T item)
Returns true if the set contains the specified item. - CopyTo(T[] array, int arrayIndex)
Copies elements into the provided array starting at the given index. - Count
Gets the number of elements currently in the set. - DefaultConcurrencyLevel
Returns the default concurrency level (typically the number of logical processors). - GetEnumerator()
Provides an enumerator for iterating over the set. - IsEmpty
Indicates whether the set contains any elements. - IsReadOnly
Always returns false—this collection is not read-only. - Remove(T item)
Removes an item if it exists. - ToArray()
Copies the elements of the ConcurrentHashSet to a new array. - TryAdd(T item)
Attempts to add the specified item to the ConcurrentHashSet. - TryPeek(T equalValue, out T actualValue)
Attempts to retrieve an item from the that equals the specified value. - TryRemove(T item)
Attempts to remove an item and returns a boolean indicating success.
Summary
If your .NET application needs a HashSet<T>-like collection that supports safe, high-performance operations across multiple threads, consider using ConcurrentHashSet<T>. It’s designed for concurrency, already in use as part of Spargine, and available via NuGet.
Try it out in your next project—and if you have feedback or ideas for improvement, I’d love to hear from you.
Get Involved!
The success of open-source projects like Spargine relies on community contributions. If you find these updates useful or have ideas for further improvements, I encourage you to contribute by:
- Submitting pull requests
- Reporting issues
- Suggesting new features
Your input is invaluable in making Spargine an even more powerful tool for the .NET community.
If you are interested in contributing or have any questions, feel free to contact me via email at dotnetdave@live.com. Your support and collaboration are greatly appreciated!
Thank you, and happy coding!
Pick up any books by David McCarter by going to Amazon.com: http://bit.ly/RockYourCodeBooks
Make a one-time donation
Make a monthly donation
Make a yearly donation
Choose an amount
Or enter a custom amount
Your contribution is appreciated.
Your contribution is appreciated.
Your contribution is appreciated.
DonateDonate monthlyDonate yearlyIf you liked this article, please buy David a cup of Coffee by going here: https://www.buymeacoffee.com/dotnetdave
© The information in this article is copywritten and cannot be preproduced in any way without express permission from David McCarter.
Discover more from dotNetTips.com
Subscribe to get the latest posts sent to your email.

