Table of Contents

Class CollectionExtensions

Namespace
MADE.Collections
Assembly
MADE.Collections.dll

Defines a collection of extensions for enumerables, lists, and collections.

public static class CollectionExtensions
Inheritance
CollectionExtensions
Inherited Members

Methods

AddIf<T>(IList<T>, T, Func<bool>)

Adds the specified item to the collection based on the specified condition being true.

public static void AddIf<T>(this IList<T> collection, T item, Func<bool> condition)

Parameters

collection IList<T>

The collection to add the item to.

item T

The item to add.

condition Func<bool>

The condition required to add the item.

Type Parameters

T

The type of item within the collection.

Exceptions

ArgumentNullException

Thrown if the collection or condition is null.

Exception

Potentially thrown by the delegate callback.

AddRangeIf<T>(ICollection<T>, IEnumerable<T>, Func<bool>)

Adds the specified collection of items to the collection based on the specified condition being true.

public static void AddRangeIf<T>(this ICollection<T> collection, IEnumerable<T> itemsToAdd, Func<bool> condition)

Parameters

collection ICollection<T>

The collection to add the items to.

itemsToAdd IEnumerable<T>

The items to add.

condition Func<bool>

The condition required to add the items.

Type Parameters

T

The type of item within the collection.

Exceptions

ArgumentNullException

Thrown if the collection, itemsToAdd or condition is null.

Exception

Potentially thrown by the delegate callback.

AddRange<T>(ICollection<T>, IEnumerable<T>)

Adds a collection of items to another.

public static void AddRange<T>(this ICollection<T> collection, IEnumerable<T> itemsToAdd)

Parameters

collection ICollection<T>

The collection to add to.

itemsToAdd IEnumerable<T>

The items to add.

Type Parameters

T

The type of items in the collection.

Exceptions

ArgumentNullException

The collection or itemsToAdd is null.

AreEquivalent<T>(ICollection<T>?, ICollection<T>?)

Determines whether two collections are equivalent, containing all the same items with no regard to order.

public static bool AreEquivalent<T>(this ICollection<T>? expected, ICollection<T>? actual)

Parameters

expected ICollection<T>

The expected collection.

actual ICollection<T>

The actual collection.

Returns

bool

True if the collections are equivalent; otherwise, false.

Type Parameters

T

The type of item.

Chunk<T>(IEnumerable<T>, int)

Chunks a collection of items into a collection of collections grouped into the specified chunk size.

public static IEnumerable<IEnumerable<T>> Chunk<T>(this IEnumerable<T> source, int chunkSize = 25)

Parameters

source IEnumerable<T>

The source collection to chunk.

chunkSize int

The chunk size.

Returns

IEnumerable<IEnumerable<T>>

A collection of collections containing the chunked items.

Type Parameters

T

The type of item.

ForEach<T>(IEnumerable<T>, Action<T>)

Performs the specified action on each item in the collection.

public static void ForEach<T>(this IEnumerable<T> collection, Action<T> action)

Parameters

collection IEnumerable<T>

The collection to action on.

action Action<T>

The action to perform.

Type Parameters

T

The type of item in the collection.

Exceptions

Exception

Potentially thrown by the delegate callback.

InsertAtPotentialIndex<T>(IList<T>, T, Func<T, T, bool>)

Inserts an item to the specified source at the potential index determined by the predicate.

public static int InsertAtPotentialIndex<T>(this IList<T> source, T value, Func<T, T, bool> predicate)

Parameters

source IList<T>

The source where the value should be inserted.

value T

The object to insert into the source.

predicate Func<T, T, bool>

The action to run to determine the position of the item based on the provided value and an item in the collection.

Returns

int

The inserted index of the item.

Type Parameters

T

The type of items in the collection.

Exceptions

NotSupportedException

The IList<T> is read-only.

IsNullOrEmpty<T>(IEnumerable<T>)

Indicates whether the specified collection is null or empty (containing no items).

public static bool IsNullOrEmpty<T>(this IEnumerable<T> source)

Parameters

source IEnumerable<T>

The collection to test.

Returns

bool

true if the source parameter is null or empty (containing no items); otherwise, false.

Type Parameters

T

The type of item in the collection.

MakeEqualTo<T>(ICollection<T>, IEnumerable<T>)

Makes the given destination collection items equal to the items in the given source collection by adding or removing items from the destination.

public static void MakeEqualTo<T>(this ICollection<T> destination, IEnumerable<T> source)

Parameters

destination ICollection<T>

The destination collection to add or remove items to.

source IEnumerable<T>

The source collection to provide the items.

Type Parameters

T

The type of item within the collection.

PotentialIndexOf<T>(IList<T>, T, Func<T, T, bool>)

Gets the potential index of an item that does not currently exist within a collection based on the specified criteria.

public static int PotentialIndexOf<T>(this IList<T> source, T value, Func<T, T, bool> predicate)

Parameters

source IList<T>

The collection to get the index from.

value T

The object to determine an index for in the source.

predicate Func<T, T, bool>

The action to run to determine the position of the item based on the provided value and an item in the collection.

Returns

int

The potential index of the item.

Type Parameters

T

The type of items in the collection.

Exceptions

Exception

Potentially thrown by the delegate callback.

RemoveIf<T>(IList<T>, T, Func<bool>)

Removes the specified item from the collection based on the specified condition being true.

public static void RemoveIf<T>(this IList<T> collection, T item, Func<bool> condition)

Parameters

collection IList<T>

The collection to remove the item from.

item T

The item to remove.

condition Func<bool>

The condition required to remove the item.

Type Parameters

T

The type of item within the collection.

Exceptions

ArgumentNullException

Thrown if the collection or condition is null.

Exception

Potentially thrown by the delegate callback.

RemoveRangeIf<T>(ICollection<T>, IEnumerable<T>, Func<bool>)

Removes the specified collection of items from the collection based on the specified condition being true.

public static void RemoveRangeIf<T>(this ICollection<T> collection, IEnumerable<T> itemsToRemove, Func<bool> condition)

Parameters

collection ICollection<T>

The collection to remove the items from.

itemsToRemove IEnumerable<T>

The items to remove.

condition Func<bool>

The condition required to remove the items.

Type Parameters

T

The type of item within the collection.

Exceptions

ArgumentNullException

Thrown if the collection, itemsToRemove or condition is null.

Exception

Potentially thrown by the delegate callback.

RemoveRange<T>(ICollection<T>, IEnumerable<T>)

Removes a collection of items from another.

public static void RemoveRange<T>(this ICollection<T> collection, IEnumerable<T> itemsToRemove)

Parameters

collection ICollection<T>

The collection to remove from.

itemsToRemove IEnumerable<T>

The items to remove from the collection.

Type Parameters

T

The type of items in the collection.

Exceptions

ArgumentNullException

The collection or itemsToRemove is null.

Shuffle<T>(IEnumerable<T>)

Shuffles the elements of a sequence randomly.

public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> source)

Parameters

source IEnumerable<T>

The collection to shuffle.

Returns

IEnumerable<T>

The shuffled collection of items.

Type Parameters

T

The type of item in the collection.

SortDescending<T, TKey>(ObservableCollection<T>, Func<T, TKey>)

Sorts the elements in the entire ObservableCollection<T> using the specified comparer in descending order.

public static void SortDescending<T, TKey>(this ObservableCollection<T> source, Func<T, TKey> comparer)

Parameters

source ObservableCollection<T>

The source collection to sort.

comparer Func<T, TKey>

The implementation to use when comparing elements.

Type Parameters

T

The type of item in the collection.

TKey

The key value of the item to sort on.

Sort<T, TKey>(ObservableCollection<T>, Func<T, TKey>)

Sorts the elements in the entire ObservableCollection<T> using the specified comparer.

public static void Sort<T, TKey>(this ObservableCollection<T> source, Func<T, TKey> comparer)

Parameters

source ObservableCollection<T>

The source collection to sort.

comparer Func<T, TKey>

The implementation to use when comparing elements.

Type Parameters

T

The type of item in the collection.

TKey

The key value of the item to sort on.

TakeFrom<T>(List<T>, int, int)

Takes a number of elements from the specified collection from the specified starting index.

public static IEnumerable<T> TakeFrom<T>(this List<T> list, int startingIndex, int takeCount)

Parameters

list List<T>

The List<T> to take items from.

startingIndex int

The index to start at in the List<T>.

takeCount int

The number of items to take from the starting index of the List<T>.

Returns

IEnumerable<T>

A collection of T items.

Type Parameters

T

The type of elements in the collection.

Update<T>(IList<T>, T, Func<T, T, bool>)

Updates an item within the collection.

public static bool Update<T>(this IList<T> collection, T item, Func<T, T, bool> predicate)

Parameters

collection IList<T>

The collection to update an item in.

item T

The item to update.

predicate Func<T, T, bool>

The function to find the item within the collection.

Returns

bool

True if the item has been updated; otherwise, false.

Type Parameters

T

The type of item within the collection.

Exceptions

ArgumentNullException

The item or collection is null.

Exception

The predicate delegate callback throws an exception.