Boost Your .NET Projects with Spargine: Unleashing the Power of EnumerableExtensions

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:

IEnumerable<T> sits at the core of modern .NET development. LINQ makes it expressive and powerful—but that convenience often comes at the cost of hidden allocations, multiple enumerations, deferred execution pitfalls, and performance surprises. Over time, these issues can quietly erode performance and complicate otherwise clean code.

The EnumerableExtensions class in the DotNetTips.Spargine.Extensions project was created to address those shortcomings. It provides a broad, performance-focused set of extension methods that make common enumerable operations faster, safer, and more explicit. These methods reduce unnecessary allocations, eliminate repetitive patterns, and provide predictable behavior—especially in performance-critical or high-throughput scenarios.

This article documents the available methods in EnumerableExtensions and explains how they can help you manage, transform, and optimize IEnumerable<T> sequences more effectively.

Methods

Below is the complete set of methods currently available in the EnumerableExtensions class.

  • AddDistinct(params Enumerable<T> items)
    Adds distinct items to the source enumerable collection, ensuring no duplicates are introduced.
  • AddFirst(T item)
    Inserts the specified item at the beginning of the collection.
  • AddIf(T item, bool condition)
    Adds the specified item to the collection if a given condition evaluates to true.
  • AddLast(T item)
    Appends the specified item to the end of the collection.
  • ContainsAny(params ReadOnlyCollection<T> items)
    Checks if the collection contains any of the specified items.
  • Count()
    Returns the total number of elements in the collection.
  • CountAsync(CancellationToken calcellationToken)
    Asynchronously counts the elements in the collection.
  • Create(bool ensureUnique)
    Creates a new collection from the specified items, with an optional parameter to ensure all items are unique.
  • EnsureUnique()
    Ensures that all elements in the collection are unique based on the default equality comparer.
  • FastAny(Funct<T, bool> accumulatorPredicate)
    Determines if any element in the sequence satisfies a specified condition.
  • FastContains(T searchItem, IComparer<T>? comparer = null)
    Performs a membership check using the most efficient strategy available for the underlying sequence.
  • FastDistinct(IEqualityComparer<T>? comparer = null)
    Returns a sequence containing only the distinct elements from the source collection with size-based optimization.
  • FastLongCount()
    Counts the elements in the collection, with an overload that accepts a predicate to filter items. Overloaded to use a function.
  • FastProcessor(Action<T> action)
    Processes each item in the collection with a specified action. Overloaded to accept a function.
  • FastShuffle()
    Randomizes the order of the elements in the collection. An overload accepts a count parameter and returns a subset of elements.
    This method is overloaded to accept a count to validate against.
  • FirstOrDefault(T alternate)
    Returns the first element of the collection or a default value if the collection is empty. Overloaded to accept a predicate.
  • FirstOrNull([DisallowNull] Func<T, bool> accumulatorPredicate)
    Returns the first element in the collection that matches the specified condition, or null if no such element is found.
  • HasDuplicates()
    Checks if the collection contains duplicate elements.
  • IndexOf(Func<T, bool> accumulatorPredicate)
    Finds the index of the first occurrence of a specified item in the collection, with an overload that accepts a comparer or a function.
    This method is overloaded to use an IEqualityComparer<T>.
  • IsEmpty()
    Determines whether the collection contains any items.
  • IsNotEmpty()
    Determines whether the specified collection has any items.
    This method is overloaded to validate against a count.
  • IsNullOrEmpty()
    Checks whether the collection is either null or empty.
  • Join(string separator)
    Concatenates the elements of the collection into a single string, using a specified separator.
  • OrderBy(string sortExpresson)
    Sorts the elements of the collection based on a specified sort expression. Overloaded to use a function.
  • OrderByOrdinal(Func<T, string> accumulatorFunction)
    Orders the elements of a collection by a key selected from each element using ordinal string comparison.
  • Page()
    Divides the collection into pages, each containing a maximum number of elements specified by the page size.
  • PageAsync(int pageSize, CancellationToken cancellationToken = default)
    Asynchronously pages the specified IAsyncEnumerable into a sequence of pages, each containing up to pageSize elements.
  • Partition(int pageCount)
    Breaks the collection into chunks of a specified size.
  • PickRandom()
    Selects a random element from the collection.
  • RemoveNulls()
    Removes any null items from the specified collection.
  • ReplaceIf(Func<T, int, bool> accumulatorPredicate, T replacement)
    Replaces elements in the collection based on a specified condition.
  • StartsWith(IEnumerable<T> second)
    Checks if the first collection starts with the same sequence as the entire second collection.
  • StructuralSequenceEqual(IEnumerable<T> second)
    Compares two sequences element-by-element using a default comparer to determine if they are structurally equal.
  • ToBlockingCollection()
    Converts the collection into a BlockingCollection.
  • ToCollection()
    Converts the collection into a Collection.
  • ToDelimitedString(char delimiter)
    Converts the collection into a string, using a delimiter to separate elements. Delimiter defaults to “,”.
  • ToFrozenSet()
    Converts the collection into a FrozenSet.
  • ToImmutableArray()
    Converts a IEnumerable<T> to ImmutableArray<T>.
  • ToLinkedList()
    Converts the collection into a LinkedList.
  • ToListAsync(CancellationToken calcellationToken)
    Asynchronously converts the collection into a List.
  • ToReadOnlyCollection()
    Converts the collection into a ReadOnlyCollection.
  • ToUniqueCollection()
    Creates a new Collection that contains only the unique elements from the source collection.
  • Upsert(T item, IEqualityComparer<T>? comparer = null)
    Inserts or updates an item in the collection. If the item already exists, it is updated; otherwise, it is added.

Summary

The EnumerableExtensions class significantly expands what you can do with IEnumerable<T> while avoiding many of the performance and correctness pitfalls commonly associated with LINQ-heavy code. By offering optimized alternatives for querying, transforming, paging, and validating sequences, these extensions make enumerable operations more explicit, predictable, and efficient.

If your applications rely heavily on collection processing—especially in performance-sensitive paths—EnumerableExtensions provides a powerful, high-performance toolkit that improves readability, reduces boilerplate, and helps your code scale with confidence.

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!

Thank you for your support, and happy coding!

Pick up any books by David McCarter by going to Amazon.com: http://bit.ly/RockYourCodeBooks

One-Time
Monthly
Yearly

Make a one-time donation

Make a monthly donation

Make a yearly donation

Choose an amount

$5.00
$15.00
$100.00
$5.00
$15.00
$100.00
$5.00
$15.00
$100.00

Or enter a custom amount

$

Your contribution is appreciated.

Your contribution is appreciated.

Your contribution is appreciated.

DonateDonate monthlyDonate yearly

If 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.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.