IListExtensions Class
Definition
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
listIList<TSource>The list to search. Must not be null.
predicateFunc<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
-1if no element satisfiespredicate.
Type Parameters
TSourceThe type of the elements in the list.
Exceptions
- ArgumentNullException
listorpredicateis 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
listIList<TSource>The list to search. Must not be null.
predicateFunc<TSource, bool>A function that defines the conditions of the element to search for.
indexintThe zero-based starting index of the search. A value equal to Count is permitted and produces an empty search;
0is valid on an empty list.
Returns
- int
The zero-based index of the first matching element in the range, or
-1if no element satisfiespredicate.
Type Parameters
TSourceThe type of the elements in the list.
Exceptions
- ArgumentNullException
listorpredicateis null.- ArgumentOutOfRangeException
indexis 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
listIList<TSource>The list to search. Must not be null.
predicateFunc<TSource, bool>A function that defines the conditions of the element to search for.
indexintThe zero-based starting index of the search. A value equal to Count is permitted and produces an empty search;
0is valid on an empty list.countintThe 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
-1if no element satisfiespredicate.
Type Parameters
TSourceThe type of the elements in the list.
Exceptions
- ArgumentNullException
listorpredicateis null.- ArgumentOutOfRangeException
indexis negative or greater than Count, orcountis negative, orindexpluscountexceeds 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
listIList<TSource>The list to search. Must not be null.
predicateFunc<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
-1if no element satisfiespredicate(including the empty-list case).
Type Parameters
TSourceThe type of the elements in the list.
Exceptions
- ArgumentNullException
listorpredicateis 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
listIList<TSource>The list to search. Must not be null.
predicateFunc<TSource, bool>A function that defines the conditions of the element to search for.
startIndexintThe 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
-1if no element satisfiespredicate.
Type Parameters
TSourceThe type of the elements in the list.
Exceptions
- ArgumentNullException
listorpredicateis null.- ArgumentOutOfRangeException
startIndexfalls 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
listIList<TSource>The list to search. Must not be null.
predicateFunc<TSource, bool>A function that defines the conditions of the element to search for.
startIndexintThe 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.countintThe 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
-1if no element satisfiespredicate.
Type Parameters
TSourceThe type of the elements in the list.
Exceptions
- ArgumentNullException
listorpredicateis null.- ArgumentOutOfRangeException
startIndexfalls outside the valid range for the current list size, orcountis negative, or the range described bystartIndexandcountextends 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
listIList<TSource>The list to modify in place. Must not be null.
newItemTSourceThe replacement value to assign to every matching slot.
predicateFunc<TSource, bool>A function that defines the conditions of the elements to replace.
Returns
- int
The number of elements that were replaced.
Type Parameters
TSourceThe 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
listorpredicateis 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
listIList<TSource>The list to modify in place. Must not be null.
oldItemTSourceThe value to locate and replace.
newItemTSourceThe replacement value.
Returns
- int
The number of elements that were replaced.
Type Parameters
TSourceThe 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
listis 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
listIList<TSource>The list to modify in place. Must not be null.
oldItemTSourceThe value to locate and replace.
newItemTSourceThe replacement value.
comparerIEqualityComparer<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
TSourceThe type of the elements in the list.
Exceptions
- ArgumentNullException
listis 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
listIList<T>The list to modify. Must not be null.
oldIndexintThe zero-based index of the item to move.
newIndexintThe zero-based insertion point in the original list before which the item will be placed. A value of
0moves 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
oldIndexornewIndexis out of range.
Type Parameters
TThe 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
listis 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
listIList<T>The list to modify. Must not be null.
indexAintThe zero-based index of the first element to swap.
indexBintThe 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
indexAorindexBis negative or greater than or equal to Count.
Type Parameters
TThe 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
listis null.
Applies to
| Product | Versions |
|---|---|
| .NET | 8, 10 |