Show / Hide Table of Contents

Class ListExtensions

Provides extension methods for List<T>s.

Inheritance
Object
ListExtensions
Namespace: NanoByte.Common.Collections
Assembly: NanoByte.Common.dll
Syntax
public static class ListExtensions : Object

Methods

AddOrReplace<T>(List<T>, T)

Adds or replaces an element in a list.

Declaration
public static bool AddOrReplace<T>(this List<T> list, T element)
Parameters
Type Name Description
List<T> list

The list to update.

T element

The element to add or update.

Returns
Type Description
Boolean
Type Parameters
Name Description
T

AddOrReplace<T, TKey>(List<T>, T, Func<T, TKey>)

Adds or replaces an element in a list using a key selector for comparison.

Declaration
public static bool AddOrReplace<T, TKey>(this List<T> list, T element, Func<T, TKey> keySelector)
Parameters
Type Name Description
List<T> list

The list to update.

T element

The element to add or update.

Func<T, TKey> keySelector

Used to map elements to keys for comparison

Returns
Type Description
Boolean
Type Parameters
Name Description
T
TKey

AddRange<TList, TElements>(IList<TList>, IEnumerable<TElements>)

Adds multiple elements to the list.

Declaration
public static void AddRange<TList, TElements>(this IList<TList> list, IEnumerable<TElements> elements)
    where TElements : TList
Parameters
Type Name Description
IList<TList> list
IEnumerable<TElements> elements
Type Parameters
Name Description
TList
TElements
Remarks

This is a covariant wrapper for AddRange(IEnumerable<T>).

GetAddedElements<T>(IList<T>, IList<T>)

Assumes two sorted lists. Determines which elements are present in newList but not in oldList.

Declaration
public static IList<T> GetAddedElements<T>(this IList<T> newList, IList<T> oldList)
    where T : IComparable<T>, IEquatable<T>
Parameters
Type Name Description
IList<T> newList

The new list of elements; can be null (will be treated as an empty list).

IList<T> oldList

The original list of elements; can be null (will be treated as an empty list).

Returns
Type Description
IList<T>

An list of elements that were added.

Type Parameters
Name Description
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>(IList<T>, IList<T>, IComparer<T>)

Assumes two sorted lists. Determines which elements are present in newList but not in oldList.

Declaration
public static IList<T> GetAddedElements<T>(this IList<T> newList, IList<T> oldList, IComparer<T> comparer)
Parameters
Type Name Description
IList<T> newList

The new list of elements; can be null (will be treated as an empty list).

IList<T> oldList

The original list of elements; can be null (will be treated as an empty list).

IComparer<T> comparer

An object that compares to elements to determine which one is bigger.

Returns
Type Description
IList<T>

An list of elements that were added.

Type Parameters
Name Description
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.

RemoveLast<T>(List<T>, Int32)

Removes the last n elements from the list.

Declaration
public static void RemoveLast<T>(this List<T> list, int number = 1)
Parameters
Type Name Description
List<T> list

The list to remove the elements from.

Int32 number

The number of elements to remove.

Type Parameters
Name Description
T
In This Article
Back to top Copyright Bastian Eicher