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
collectionIList<T>The collection to add the item to.
itemTThe item to add.
conditionFunc<bool>The condition required to add the item.
Type Parameters
TThe type of item within the collection.
Exceptions
- ArgumentNullException
Thrown if the
collectionorconditionis 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
collectionICollection<T>The collection to add the items to.
itemsToAddIEnumerable<T>The items to add.
conditionFunc<bool>The condition required to add the items.
Type Parameters
TThe type of item within the collection.
Exceptions
- ArgumentNullException
Thrown if the
collection,itemsToAddorconditionis 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
collectionICollection<T>The collection to add to.
itemsToAddIEnumerable<T>The items to add.
Type Parameters
TThe type of items in the collection.
Exceptions
- ArgumentNullException
The
collectionoritemsToAddis 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
expectedICollection<T>The expected collection.
actualICollection<T>The actual collection.
Returns
- bool
True if the collections are equivalent; otherwise, false.
Type Parameters
TThe 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
sourceIEnumerable<T>The source collection to chunk.
chunkSizeintThe chunk size.
Returns
- IEnumerable<IEnumerable<T>>
A collection of collections containing the chunked items.
Type Parameters
TThe 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
collectionIEnumerable<T>The collection to action on.
actionAction<T>The action to perform.
Type Parameters
TThe 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
sourceIList<T>The source where the
valueshould be inserted.valueTThe object to insert into the
source.predicateFunc<T, T, bool>The action to run to determine the position of the item based on the provided
valueand an item in the collection.
Returns
- int
The inserted index of the item.
Type Parameters
TThe 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
sourceIEnumerable<T>The collection to test.
Returns
Type Parameters
TThe 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
destinationICollection<T>The destination collection to add or remove items to.
sourceIEnumerable<T>The source collection to provide the items.
Type Parameters
TThe 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
sourceIList<T>The collection to get the index from.
valueTThe object to determine an index for in the
source.predicateFunc<T, T, bool>The action to run to determine the position of the item based on the provided
valueand an item in the collection.
Returns
- int
The potential index of the item.
Type Parameters
TThe 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
collectionIList<T>The collection to remove the item from.
itemTThe item to remove.
conditionFunc<bool>The condition required to remove the item.
Type Parameters
TThe type of item within the collection.
Exceptions
- ArgumentNullException
Thrown if the
collectionorconditionis 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
collectionICollection<T>The collection to remove the items from.
itemsToRemoveIEnumerable<T>The items to remove.
conditionFunc<bool>The condition required to remove the items.
Type Parameters
TThe type of item within the collection.
Exceptions
- ArgumentNullException
Thrown if the
collection,itemsToRemoveorconditionis 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
collectionICollection<T>The collection to remove from.
itemsToRemoveIEnumerable<T>The items to remove from the collection.
Type Parameters
TThe type of items in the collection.
Exceptions
- ArgumentNullException
The
collectionoritemsToRemoveis null.
Shuffle<T>(IEnumerable<T>)
Shuffles the elements of a sequence randomly.
public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> source)
Parameters
sourceIEnumerable<T>The collection to shuffle.
Returns
- IEnumerable<T>
The shuffled collection of items.
Type Parameters
TThe 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
sourceObservableCollection<T>The source collection to sort.
comparerFunc<T, TKey>The implementation to use when comparing elements.
Type Parameters
TThe type of item in the collection.
TKeyThe 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
sourceObservableCollection<T>The source collection to sort.
comparerFunc<T, TKey>The implementation to use when comparing elements.
Type Parameters
TThe type of item in the collection.
TKeyThe 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
listList<T>The List<T> to take items from.
startingIndexintThe index to start at in the List<T>.
takeCountintThe number of items to take from the starting index of the List<T>.
Returns
- IEnumerable<T>
A collection of
Titems.
Type Parameters
TThe 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
collectionIList<T>The collection to update an item in.
itemTThe item to update.
predicateFunc<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
TThe type of item within the collection.
Exceptions
- ArgumentNullException
The
itemorcollectionis null.- Exception
The
predicatedelegate callback throws an exception.