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 CollectionExtensions class in the DotNetTips.Spargine.Extensions project was created to eliminate that friction. It provides a focused set of high-performance, defensive, and developer-friendly extension methods that enhance ICollection<T> without changing how you think about collections. These extensions reduce boilerplate code, enforce safer usage patterns, and unlock modern .NET performance features such as spans and frozen collections—all while remaining easy to adopt in existing projects. This article documents the available methods in CollectionExtensions and explains how they can simplify your code while improving correctness and runtime efficiency
Methods
Here is a list of the current methods available in the CollectionExtensions class:
- AddIf(T item, bool condition)
Adds an item to the ICollection if a specified condition is met. It validates that both the collection and the item to be added are not null. - AddIfNotExists(T item, IEqualityComparer<T>? comparer = null)
Adds the specified item to the collection if it does not already exist. - AddRange(IEnumerable<T> items, bool ensureUnique)
Adds multiple items to the ICollection. It validates that both the collection and the items are not null. The ensureUnique parameter ensures that the items added are unique. - AsReadOnlySpan()
Converts a collection to a ReadOnlySpan, which may improve performance. - AsSpan()
Converts the collection to a Span, which may improve performance. - ToFrozenSet()
Converts a collection to a FrozenSet, which may improve performance. - Upsert<T, TKey>(item)
Upserts (updates or inserts) the specified item into the collection.
Summary
The CollectionExtensions class is designed to make everyday collection operations safer, cleaner, and faster. By eliminating repetitive patterns, enforcing defensive checks, and embracing modern .NET performance features, these extensions help you write code that is easier to maintain and more efficient at runtime.
If your applications rely heavily on collections—and most do—these enhancements can deliver immediate gains in code quality, reliability, and performance with minimal effort.
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.

