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

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:

The ArrayExtensions class in the DotNetTips.Spargine.Extensions project delivers a powerful set of performance-focused helpers designed to make working with arrays in .NET cleaner, faster, and more expressive. Arrays are already one of the most efficient collection types, and with Spargine’s optimized extensions, you can unlock even more speed, reliability, and developer productivity.

Below is a detailed breakdown of the currently available methods.

Methods

Adding & Updating Items

  • AddFirst(T item)
    Adds an item to the beginning of the array.
  • AddIf(T item, bool condition)
    Adds an item to the array only when the specified condition evaluates to true.
  • AddLast(T item)
    Adds an item to the end of the array.
  • Upsert(T item)
    Adds the item if it does not exist in the array — otherwise updates the existing matching item.

Comparison & Hashing

  • AreEqual(T[] arrayToCheck)
    Determines whether the current array and the specified array contain the same elements.
  • GenerateHashCode()
    Generates a hash code representing the entire array.
  • FastHashData()
    Computes a fast SHA256 hash for the given byte array.

Conversion & Projection

  • AsReadOnlySpan()
    Converts the array into a ReadOnlySpan<T> for allocation-free access.
  • BytesToString()
    Converts a byte array to a string using a pooled StringBuilder to minimize allocations.
  • FastSelectItems(int startIndex, int count)
    Returns a range of items using optimized ArraySegment slicing.
  • ToDistinct()
    Returns a new array with duplicate elements removed.
  • ToFrozenSet()
    Converts the array to a FrozenSet<T> for potential lookup performance improvements.

Searching & Matching

  • ContainsAny(params T[] items)
    Returns true if the array contains any of the specified values.
  • IndexOf(T item)
    Returns the zero-based index of the first matching element.
  • LastIndexOf(T item)
    Returns the zero-based index of the last matching element.

State & Validation

  • IsEmpty()
    Returns true if the array contains no elements.
  • IsNotEmpty()
    Returns true if the array contains at least one element.
  • IsNotEmpty(Func<T, bool> predicate)
    Returns true if any element satisfies the supplied condition.

Iteration & Processing

  • FastProcessor(Action<T> action)
    Executes the given action on each element using an optimized loop.
  • FastLongCount()
    Returns the number of elements using the fastest benchmark-verified approach.
  • PerformAction(Action<T> action)
    Executes the specified action on every element in the array.

Cloning & Removing

  • FastClone()
    Creates a shallow clone of the array using optimized internal mechanics.
  • RemoveFirst()
    Removes the first element and returns a new array without it.
  • RemoveLast()
    Removes the last element and returns a new array without it.

Summary

ArrayExtensions brings high-impact, performance-driven utilities to one of .NET’s most fundamental data structures. Whether you’re optimizing hot paths, improving expressiveness, or reducing memory allocations, these extensions help ensure your array operations remain clean, predictable, and lightning-fast.

If you care about performance, Spargine has your back.

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

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.