Table of Contents

IListExtensions Class

Definition

Namespace
Bodu.Collections.Generic.Extensions
Assembly
Bodu.Core.dll
Package
Bodu.Core 1.0.1
Source
IListExtensions.IndexOf.cs

Provides extension methods that add predicate-based search, safe relocation, and bulk replacement operations to any type implementing IList<T>.

public static class IListExtensions
Inheritance
IListExtensions
Inherited Members

Examples

IList<string> items = new List<string> { "alpha", "beta", "gamma", "beta" };

// Predicate-based search.
int firstBetaIndex = items.IndexOf(s => s.StartsWith("b")); // 1

// Try-style relocation that won't throw on out-of-range indices.
if (items.TryMove(oldIndex: 0, newIndex: 2))
    Console.WriteLine(string.Join(", ", items)); // beta, gamma, alpha, beta

// Bulk replacement with a count of changes.
int replaced = items.ReplaceAll(oldItem: "beta", newItem: "BETA"); // 2

Remarks

The extension surface fills gaps in the BCL IList<T> contract. Predicate variants of IndexOf(T) and LastIndexOf(T) let callers locate elements by an arbitrary condition without first projecting the list through LINQ; TryMove and TrySwap reorder elements in-place with bounds-checked, non-throwing semantics; and ReplaceAll performs an in-place value substitution across the entire list (or matched subset) and returns the number of replacements made.

All methods operate directly against the supplied list - no copy is allocated, and the original ordering of non-affected elements is preserved. Methods that mutate require the list to be writable; methods that only search are safe to call on read-only views provided the list still implements IList<T>.

Equality-based methods accept an optional IEqualityComparer<T>. When omitted, Default is used, matching the BCL convention so behaviour is consistent with the rest of the framework.

Methods

IndexOf<TSource>(IList<TSource>, Func<TSource, bool>)

Returns the zero-based index of the first element in the entire IList<T> that satisfies the specified predicate.

public static int IndexOf<TSource>(this IList<TSource> list, Func<TSource, bool> predicate)

Parameters

list IList<TSource>

The list to search. Must not be null.

predicate Func<TSource, bool>

A function that defines the conditions of the element to search for.

Returns

int

The zero-based index of the first matching element, or -1 if no element satisfies predicate.

Type Parameters

TSource

The type of the elements in the list.

Exceptions

ArgumentNullException

list or predicate is null.

IndexOf<TSource>(IList<TSource>, Func<TSource, bool>, int)

Returns the zero-based index of the first element that satisfies the specified predicate within the range of elements in the IList<T> that extends from the specified index to the end of the list.

public static int IndexOf<TSource>(this IList<TSource> list, Func<TSource, bool> predicate, int index)

Parameters

list IList<TSource>

The list to search. Must not be null.

predicate Func<TSource, bool>

A function that defines the conditions of the element to search for.

index int

The zero-based starting index of the search. A value equal to Count is permitted and produces an empty search; 0 is valid on an empty list.

Returns

int

The zero-based index of the first matching element in the range, or -1 if no element satisfies predicate.

Type Parameters

TSource

The type of the elements in the list.

Exceptions

ArgumentNullException

list or predicate is null.

ArgumentOutOfRangeException

index is negative or greater than Count.

IndexOf<TSource>(IList<TSource>, Func<TSource, bool>, int, int)

Returns the zero-based index of the first element that satisfies the specified predicate within the range of elements in the IList<T> that starts at the specified index and contains the specified number of elements.

public static int IndexOf<TSource>(this IList<TSource> list, Func<TSource, bool> predicate, int index, int count)

Parameters

list IList<TSource>

The list to search. Must not be null.

predicate Func<TSource, bool>

A function that defines the conditions of the element to search for.

index int

The zero-based starting index of the search. A value equal to Count is permitted and produces an empty search; 0 is valid on an empty list.

count int

The number of elements to examine. Must be non-negative and must not extend past the end of the list when added to index.

Returns

int

The zero-based index of the first matching element in the range, or -1 if no element satisfies predicate.

Type Parameters

TSource

The type of the elements in the list.

Exceptions

ArgumentNullException

list or predicate is null.

ArgumentOutOfRangeException

index is negative or greater than Count, or count is negative, or index plus count exceeds Count.

LastIndexOf<TSource>(IList<TSource>, Func<TSource, bool>)

Returns the zero-based index of the last element in the entire IList<T> that satisfies the specified predicate, searching backwards from the end of the list.

public static int LastIndexOf<TSource>(this IList<TSource> list, Func<TSource, bool> predicate)

Parameters

list IList<TSource>

The list to search. Must not be null.

predicate Func<TSource, bool>

A function that defines the conditions of the element to search for.

Returns

int

The zero-based index of the last matching element, or -1 if no element satisfies predicate (including the empty-list case).

Type Parameters

TSource

The type of the elements in the list.

Exceptions

ArgumentNullException

list or predicate is null.

LastIndexOf<TSource>(IList<TSource>, Func<TSource, bool>, int)

Returns the zero-based index of the last element that satisfies the specified predicate within the range of elements in the IList<T> that extends from the first element to the specified index.

public static int LastIndexOf<TSource>(this IList<TSource> list, Func<TSource, bool> predicate, int startIndex)

Parameters

list IList<TSource>

The list to search. Must not be null.

predicate Func<TSource, bool>

A function that defines the conditions of the element to search for.

startIndex int

The zero-based starting index of the backward search. For a non-empty list this must be in the range [0, Count - 1]; for an empty list the only valid value is -1.

Returns

int

The zero-based index of the last matching element in the range, or -1 if no element satisfies predicate.

Type Parameters

TSource

The type of the elements in the list.

Exceptions

ArgumentNullException

list or predicate is null.

ArgumentOutOfRangeException

startIndex falls outside the valid range for the current list size.

LastIndexOf<TSource>(IList<TSource>, Func<TSource, bool>, int, int)

Returns the zero-based index of the last element that satisfies the specified predicate within the range of elements in the IList<T> that contains the specified number of elements and ends at the specified index.

public static int LastIndexOf<TSource>(this IList<TSource> list, Func<TSource, bool> predicate, int startIndex, int count)

Parameters

list IList<TSource>

The list to search. Must not be null.

predicate Func<TSource, bool>

A function that defines the conditions of the element to search for.

startIndex int

The zero-based starting index of the backward search. For a non-empty list this must be in the range [0, Count - 1]; for an empty list the only valid value is -1.

count int

The number of elements to examine. Must be non-negative and must not extend before the start of the list relative to startIndex.

Returns

int

The zero-based index of the last matching element in the range, or -1 if no element satisfies predicate.

Type Parameters

TSource

The type of the elements in the list.

Exceptions

ArgumentNullException

list or predicate is null.

ArgumentOutOfRangeException

startIndex falls outside the valid range for the current list size, or count is negative, or the range described by startIndex and count extends past the beginning of the list.

ReplaceAll<TSource>(IList<TSource>, TSource, Func<TSource, bool>)

Replaces every element in the IList<T> that satisfies the specified predicate with newItem.

public static int ReplaceAll<TSource>(this IList<TSource> list, TSource newItem, Func<TSource, bool> predicate)

Parameters

list IList<TSource>

The list to modify in place. Must not be null.

newItem TSource

The replacement value to assign to every matching slot.

predicate Func<TSource, bool>

A function that defines the conditions of the elements to replace.

Returns

int

The number of elements that were replaced.

Type Parameters

TSource

The type of the elements in the list.

Remarks

The list is walked once with a fixed length captured before iteration begins. Predicate matches against newItem do not cause additional passes or infinite recursion: a slot is tested exactly once.

Exceptions

ArgumentNullException

list or predicate is null.

ReplaceAll<TSource>(IList<TSource>, TSource, TSource)

Replaces every occurrence of oldItem in the IList<T> with newItem, using Default to compare elements.

public static int ReplaceAll<TSource>(this IList<TSource> list, TSource oldItem, TSource newItem)

Parameters

list IList<TSource>

The list to modify in place. Must not be null.

oldItem TSource

The value to locate and replace.

newItem TSource

The replacement value.

Returns

int

The number of elements that were replaced.

Type Parameters

TSource

The type of the elements in the list.

Remarks

Elements are assigned in place through the list's indexer; no insertions or removals are performed, so the list size and the relative position of every element are preserved.

Exceptions

ArgumentNullException

list is null.

ReplaceAll<TSource>(IList<TSource>, TSource, TSource, IEqualityComparer<TSource>?)

Replaces every occurrence of oldItem in the IList<T> with newItem, using the supplied IEqualityComparer<T> to compare elements.

public static int ReplaceAll<TSource>(this IList<TSource> list, TSource oldItem, TSource newItem, IEqualityComparer<TSource>? comparer)

Parameters

list IList<TSource>

The list to modify in place. Must not be null.

oldItem TSource

The value to locate and replace.

newItem TSource

The replacement value.

comparer IEqualityComparer<TSource>

The equality comparer used to locate occurrences of oldItem. When null, Default is used.

Returns

int

The number of elements that were replaced.

Type Parameters

TSource

The type of the elements in the list.

Exceptions

ArgumentNullException

list is null.

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

Attempts to move an item from one index to another within the list.

public static bool TryMove<T>(this IList<T> list, int oldIndex, int newIndex)

Parameters

list IList<T>

The list to modify. Must not be null.

oldIndex int

The zero-based index of the item to move.

newIndex int

The zero-based insertion point in the original list before which the item will be placed. A value of 0 moves the item to the front. A value equal to Count moves the item to the end of the list.

Returns

bool

true if the move was performed or the item was already in the correct position; false if oldIndex or newIndex is out of range.

Type Parameters

T

The type of elements in the list.

Remarks

The newIndex parameter represents an insertion point in the original list, not a final target index. The item is conceptually placed before the element currently at newIndex , after which all subsequent elements shift to accommodate it. This means the item's final index in the result list will be newIndex when moving left (or to the same relative position), but newIndex minus one when moving right.

The method returns true without modifying the list when the move would produce no observable change - specifically when oldIndex equals newIndex (same position), or when oldIndex equals newIndex minus one (the item is already directly before the insertion point and shifting it right would leave it in the same position after the index adjustment for removal).

Valid values for oldIndex are in the range [0, Count). Valid values for newIndex are in the range [0, Count], where a value of Count specifies insertion at the end of the list.

Exceptions

ArgumentNullException

Thrown if list is null.

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

Attempts to swap the elements at the specified indexes in the list.

public static bool TrySwap<T>(this IList<T> list, int indexA, int indexB)

Parameters

list IList<T>

The list to modify. Must not be null.

indexA int

The zero-based index of the first element to swap.

indexB int

The zero-based index of the second element to swap.

Returns

bool

true if the swap was performed or both indexes refer to the same element; false if either indexA or indexB is negative or greater than or equal to Count.

Type Parameters

T

The type of elements in the list.

Remarks

When indexA and indexB are equal, the method returns true without modifying the list.

Exceptions

ArgumentNullException

Thrown if list is null.

Applies to

ProductVersions
.NET8, 10