Class ListExtensions
- Namespace
- Cuemon.Extensions.Collections.Generic
- Assembly
- Cuemon.Extensions.Collections.Generic.dll
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
HasIndex<T>(IList<T>, int)
Determines whether the list of the IList<T> is within the range of the index.
public static bool HasIndex<T>(this IList<T> list, int index)
Parameters
Returns
- bool
trueif the specifiedindexis within the range of thelist; otherwise,false.
Type Parameters
TThe type of elements in the IList<T>.
Exceptions
- ArgumentNullException
listis null.
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.
public static T Next<T>(this IList<T> list, int index)
Parameters
listIList<T>The IList<T> to extend.
indexintThe index of which to advance to the next element from.
Returns
- T
default(TSource) if
indexis equal or greater than Count; otherwise the next element oflistrelative toindex.
Type Parameters
TThe type of elements in the IList<T>.
Exceptions
- ArgumentNullException
listis null.- ArgumentOutOfRangeException
indexis less than 0.
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.
public static T Previous<T>(this IList<T> list, int index)
Parameters
listIList<T>The IList<T> to extend.
indexintThe index of which to advance to the previous element from.
Returns
- T
default(TSource) if
indexis equal, greater or lower than Count; otherwise the previous element oflistrelative toindex.
Type Parameters
TThe type of elements in the IList<T>.
Exceptions
- ArgumentNullException
listis null.- ArgumentOutOfRangeException
indexis less than 0.
Remove<T>(IList<T>, Func<T, bool>)
Removes the first occurrence of a specific object from the list.
public static bool Remove<T>(this IList<T> list, Func<T, bool> predicate)
Parameters
listIList<T>The IList<T> to extend.
predicateFunc<T, bool>The function delegate that defines the conditions of the element to remove.
Returns
- bool
trueif item was successfully removed from thelist,falseotherwise.
Type Parameters
TThe type of elements in the IList<T>.
TryAdd<T>(IList<T>, T)
Attempts to add the specified item to the list.
public static bool TryAdd<T>(this IList<T> list, T item)
Parameters
Returns
- bool
trueif the item was added to thelistsuccessfully,falseotherwise.
Type Parameters
T
Remarks
This method will add the specified item to the list if it is not already present.