Table of Contents

ListExtensions Class

Definition

Namespace
Cuemon.Extensions.Collections.Generic
Assemblies
Cuemon.Extensions.Collections.Generic.dll
Source
src/Cuemon.Extensions.Collections.Generic/ListExtensions.cs

Extension methods for the IList<T> interface.

public static class ListExtensions
Inheritance
ListExtensions

Examples

ListExtensions provides extension methods for List<T> for safe navigation and manipulation including predicate-based removal, bounds checking, adjacent-element access, and conditional addition. This example creates a list of fruit names ["Apple", "Banana", "Cherry", "Date"], calls Remove with a predicate to delete "Banana", checks whether index 5 exists with HasIndex, and retrieves adjacent elements using Next(0) and Previous(2) without throwing on out-of-bounds access. TryAdd conditionally adds "Cherry" (duplicate, returns false) and "Elderberry" (new, returns true). Console output confirms the removal, bounds-check results, neighbor values, and the success of each conditional addition.

using System;
using System.Collections.Generic;
using Cuemon.Extensions.Collections.Generic;

namespace DocExamples
{
    public static class ListExtensionsExample
    {
        public static void Main()
        {
            var fruits = new List<string> { "Apple", "Banana", "Cherry", "Date" };

            // Remove the first element matching a condition
            bool removed = fruits.Remove(f => f == "Banana");
            Console.WriteLine($"Removed 'Banana': {removed}");
            Console.WriteLine($"Fruits after removal: {string.Join(", ", fruits)}");

            // Check if an index exists in the list
            bool hasIndex = fruits.HasIndex(5);
            Console.WriteLine($"Has index 5: {hasIndex}");
            Console.WriteLine($"Has index 1: {fruits.HasIndex(1)}");

            // Get the next element relative to an index
            string next = fruits.Next(0);
            Console.WriteLine($"Element after index 0: {next}");

            // Get the previous element relative to an index
            string prev = fruits.Previous(2);
            Console.WriteLine($"Element before index 2: {prev}");

            // Returns default when out of bounds
            string beyond = fruits.Next(10);
            Console.WriteLine($"Element after index 10: {(beyond == null ? "null (default)" : beyond)}");

            // Try to add an element (only if not already present)
            bool added = fruits.TryAdd("Cherry");
            Console.WriteLine($"Added 'Cherry' (duplicate): {added}");

            bool addedNew = fruits.TryAdd("Elderberry");
            Console.WriteLine($"Added 'Elderberry' (new): {addedNew}");
            Console.WriteLine($"Fruits: {string.Join(", ", fruits)}");

}}
}

Methods

Name Description
HasIndex<T>(IList<T>, int)

Determines whether the list of the IList<T> is within the range of the index.

Next<T>(IList<T>, int)

Returns the next element of list relative to index, or the last element of list if index is equal or greater than Count.

Previous<T>(IList<T>, int)

Returns the previous element of list relative to index, or the first or last element of list if index is equal, greater or lower than Count.

Remove<T>(IList<T>, Func<T, bool>)

Removes the first occurrence of a specific object from the list.

TryAdd<T>(IList<T>, T)

Attempts to add the specified item to the list.