Class ListExtensions
- Namespace
- NanoByte.Common.Collections
- Assembly
- NanoByte.Common.dll
Provides extension methods for System.Collections.Generic.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
list
List<T>The list to update.
element
TThe element to add or update.
Returns
- bool
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
list
List<T>The list to update.
element
TThe element to add or update.
keySelector
Func<T, TKey>Used to map elements to keys for comparison
Returns
- bool
Type Parameters
T
TKey
AddRange<TList, TElements>(IList<TList>, IEnumerable<TElements>)
Adds multiple elements to the list.
public static void AddRange<TList, TElements>(this IList<TList> list, IEnumerable<TElements> elements) where TElements : TList
Parameters
list
IList<TList>elements
IEnumerable<TElements>
Type Parameters
TList
TElements
Remarks
This is a covariant wrapper for System.Collections.Generic.List<T>.AddRange(System.Collections.Generic.IEnumerable<T>).
GetAddedElements<T>(IList<T>?, IList<T>?)
Assumes two sorted lists. Determines which elements are present in newList
but not in oldList
.
[Pure]
public static IList<T> GetAddedElements<T>(this IList<T>? newList, IList<T>? oldList) where T : IComparable<T>, IEquatable<T>
Parameters
newList
IList<T>The new list of elements; can be
null
(will be treated as an empty list).oldList
IList<T>The original list of elements; can be
null
(will be treated as an empty list).
Returns
- IList<T>
An 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 System.IComparable<T>.CompareTo(T) but have been otherwise modified will be added.
GetAddedElements<T>(IList<T>?, IList<T>?, IComparer<T>)
Assumes two sorted lists. Determines which elements are present in newList
but not in oldList
.
[Pure]
public static IList<T> GetAddedElements<T>(this IList<T>? newList, IList<T>? oldList, IComparer<T> comparer)
Parameters
newList
IList<T>The new list of elements; can be
null
(will be treated as an empty list).oldList
IList<T>The original list of elements; can be
null
(will be treated as an empty list).comparer
IComparer<T>An object that compares to elements to determine which one is bigger.
Returns
- IList<T>
An 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 System.IComparable<T>.CompareTo(T) but have been otherwise modified will be added.
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
list
List<T>The list to remove the elements from.
number
intThe number of elements to remove.
Type Parameters
T