Class ListExtensions
- Namespace
- NanoByte.Common.Collections
- Assembly
- NanoByte.Common.dll
Provides extension methods for List<T>s.
public static class ListExtensions
- Inheritance
-
objectListExtensions
Methods
AddOrReplace<T>(List<T>, T)
Adds or replaces an element in a list.
public static bool AddOrReplace<T>(this List<T> list, T element)
Parameters
listList<T>elementTThe element to add or update.
Returns
Type Parameters
T
AddOrReplace<T, TKey>(List<T>, T, Func<T, TKey>)
Adds or replaces an element in a list using a key selector for comparison.
public static bool AddOrReplace<T, TKey>(this List<T> list, T element, Func<T, TKey> keySelector)
Parameters
listList<T>elementTThe element to add or update.
keySelectorFunc<T, TKey>Used to map elements to keys for comparison
Returns
Type Parameters
TTKey
AddRange<T, TElement>(IList<T>, IEnumerable<TElement>)
Adds multiple elements to the list.
public static void AddRange<T, TElement>(this IList<T> list, IEnumerable<TElement> elements) where TElement : T
Parameters
listIList<T>elementsIEnumerable<TElement>
Type Parameters
TTElement
Remarks
This is a covariant wrapper for AddRange(IEnumerable<T>).
GetAddedElements<T>(IReadOnlyList<T>?, IReadOnlyList<T>?)
Assumes two sorted lists. Determines which elements are present in newList but not in oldList.
public static IReadOnlyList<T> GetAddedElements<T>(this IReadOnlyList<T>? newList, IReadOnlyList<T>? oldList) where T : IComparable<T>, IEquatable<T>
Parameters
newListIReadOnlyList<T>The new list of elements; can be
null(will be treated as an empty list).oldListIReadOnlyList<T>The original list of elements; can be
null(will be treated as an empty list).
Returns
- IReadOnlyList<T>
A list of elements that were added.
Type Parameters
T
Remarks
Elements that are present in oldList but not in newList are ignored. Elements that are equal for CompareTo(T) but have been otherwise modified will be added.
GetAddedElements<T>(IReadOnlyList<T>?, IReadOnlyList<T>?, IComparer<T>)
Assumes two sorted lists. Determines which elements are present in newList but not in oldList.
public static IReadOnlyList<T> GetAddedElements<T>(this IReadOnlyList<T>? newList, IReadOnlyList<T>? oldList, IComparer<T> comparer)
Parameters
newListIReadOnlyList<T>The new list of elements; can be
null(will be treated as an empty list).oldListIReadOnlyList<T>The original list of elements; can be
null(will be treated as an empty list).comparerIComparer<T>An object that compares to elements to determine which one is bigger.
Returns
- IReadOnlyList<T>
A list of elements that were added.
Type Parameters
T
Remarks
Elements that are present in oldList but not in newList are ignored. Elements that are equal for CompareTo(T) but have been otherwise modified will be added.
IndexOfByReference<T>(IList<T>, T)
Determines the index of a specific element instance in a list, comparing by reference identity.
public static int IndexOfByReference<T>(this IList<T> list, T element)
Parameters
listIList<T>elementTThe element instance to locate.
Returns
- int
The index of
elementin thelist;-1if not found.
Type Parameters
T
Remarks
Unlike IndexOf(T) this targets the exact instance even when the list contains other elements that are equal by value.
RemoveLast<T>(List<T>, int)
Removes the last n elements from the list.
public static void RemoveLast<T>(this List<T> list, int number = 1)
Parameters
Type Parameters
T