Boost Your .NET Projects with Spargine: Unlocking the Power of ListExtensions

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 ListExtensions class is part of the DotNetTips.Spargine.Core assembly provides an extensive set of high-performance methods to enhance the functionality of the List<T> type. These methods have been rigorously tested and benchmarked to ensure optimal efficiency and reliability.

Features and Methods

The ListExtensions class includes a variety of methods designed to improve usability and performance:

Action Execution

  • PerformAction(Action<T> action)
    Executes an action on each item in the list.

Adding Elements

  • AddFirst(T item)
    Adds an item to the beginning of the list.
  • AddLast(T item)
    Adds an item to the end of the list.
  • AddRangeIfNotExists(IEnumerable<T> items, IEqualityComparer<T>? comparer = null)
    Adds a range of items to the list if they do not already exist in the list, using a custom equality comparer.

Counting

  • FastCount()
    Counts the number of elements in the specified list.

Collection Conversions

  • ToCollection()
    Converts the list into a Collection<T>.
  • ToDistinctBlockingCollection(bool completeAdding)
    Converts the list into a DistinctBlockingCollection<T> (a custom Spargine type).
  • ToDistinctConcurrentBag()
    Converts the list into a DistinctConcurrentBag<T> (a custom Spargine type).
  • ToFastSortedList()
    Converts the list into a FastSortedList<T> (a custom Spargine type). Method is overloaded to use an IComparer<T>.
  • ToFrozenSet()
    Converts the list into a FrozenSet<T>.
  • ToImmutableArray()
    Converts the list into an ImmutableArray<T>.
  • ToListAsync(CancellationToken cancellationToken)
    Asynchronously converts an IAsyncEnumerable<T> to a List<T>.
  • ToObservable()
    Converts the list into an ObservableList<T>.
  • ToObservableCollection()
    Converts the list into an ObservableCollection<T>.
  • ToReadOnly()
    Converts the list into a IReadOnlyList<T>, a preferred return type for public APIs.
  • ToReadOnlyObservableCollection()
    Converts the list into a ReadOnlyObservableCollection<T>.

Hashing

  • GenerateHashCode()
    Computes a hash code for the entire list based on its elements.

Indexing and Lookup

  • IndexAtLooped(int index)
    Efficiently finds an index while avoiding multiple enumerations.

Item Removal and Cleanup

  • ClearNulls()
    Removes all null elements from the list.
  • RemoveFirst(T item)
    Removes the first occurrence of a specified item.
  • RemoveLast(T item)
    Removes the last occurrence of a specified item.

Shuffling and Splitting

  • FastShuffle()
    Randomly rearranges the elements of the list.
  • Split(int size)
    Divides the list into smaller sublists of a specified size.

Span-Based Performance Enhancements

  • AsReadOnlySpan()
    Creates a ReadOnlySpan<T> from the list, enhancing performance during iteration.
  • AsSpan()
    Generates a Span<T> using CollectionsMarshal.AsSpan(), improving performance when modifying elements.

Validation and Comparison

  • IsEmpty()
    Determines whether the List contains any elements.
  • IsEqualTo(List<T> collectionToCheck)
    Determines whether two lists are equal.
  • IsNotEmpty()
    Determines whether the specified List contains any items. This method is overloaded to also use a Predicate<T> or a count.

Reliability and Exception Handling

All methods in ListExtensions validate input parameters to minimize errors and ensure robust functionality. Exceptions thrown by these methods are well-documented to assist developers in debugging and error handling.

By leveraging ListExtensions, developers can enhance performance, improve code maintainability, and reduce redundancy when working with lists in .NET applications.

Summary

The ListExtensions class in Spargine delivers a powerful, performance-focused toolkit for working with List<T> in .NET. By extending native list capabilities with high-efficiency operations, span-based optimization, rich collection conversion helpers, safe validation, and robust exception handling, these extensions help developers write cleaner, faster, and more maintainable code. Whether you’re streamlining data processing, improving memory usage, or reducing boilerplate logic, ListExtensions provides proven, benchmarked solutions you can rely on in real-world applications. If you want to get more out of your collections—and your .NET projects as a whole—Spargine’s ListExtensions is an invaluable resource to have in your toolbox.

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 reproduced 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 Reply