Table of Contents

Class EnumerableExtensions

Namespace
Plugin.BaseTypeExtensions
Assembly
Plugin.BaseTypeExtensions.dll

Provides extension methods for working with IEnumerable and IEnumerable<T>.

public static class EnumerableExtensions
Inheritance
EnumerableExtensions
Inherited Members

Methods

Enqueue<T>(ConcurrentQueue<T>, T, int)

Enqueues an item to the queue and ensures the queue does not exceed the specified maximum size.

public static void Enqueue<T>(this ConcurrentQueue<T> queue, T obj, int max)

Parameters

queue ConcurrentQueue<T>

The queue to enqueue the item to. Cannot be null.

obj T

The item to enqueue. Can be null.

max int

The maximum size of the queue.

Type Parameters

T

GetOrDefault<T>(IEnumerable<T?>?, int, T?)

Gets the element at the specified index or returns the default value if the index is out of range.

public static T? GetOrDefault<T>(this IEnumerable<T?>? enumerable, int index, T? defaultValue = default)

Parameters

enumerable IEnumerable<T>

The enumerable to get the element from. Can be null.

index int

The index of the element to get. If the index is out of range or if the enumerable is null, the default value is returned.

defaultValue T

The default value to return if the index is out of range. Can be null.

Returns

T

Type Parameters

T

NullIfEmpty(IEnumerable?)

Coalesces the input to null if it is empty.

public static IEnumerable? NullIfEmpty(this IEnumerable? input)

Parameters

input IEnumerable

The input sequence to coalesce. Can be null.

Returns

IEnumerable

The input sequence that was passed in if it is not empty; otherwise, null.

NullIfEmpty<T>(IEnumerable<T?>?)

Coalesces the input to null if it is empty.

public static IEnumerable<T?>? NullIfEmpty<T>(this IEnumerable<T?>? input)

Parameters

input IEnumerable<T>

The input sequence to coalesce. Can be null.

Returns

IEnumerable<T>

The input sequence that was passed in if it is not empty; otherwise, null.

Type Parameters

T

PickRandom<T>(IEnumerable<T>)

Picks a random element from the collection.

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

Parameters

source IEnumerable<T>

Returns

T

Type Parameters

T

Exceptions

InvalidOperationException

Thrown if the source collection is empty.

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

Picks a specified number of random elements from the collection.

public static IEnumerable<T> PickRandom<T>(this IEnumerable<T> source, int count)

Parameters

source IEnumerable<T>
count int

Returns

IEnumerable<T>

Type Parameters

T

Exceptions

ArgumentException

Thrown if requested count exceeds the source collection count.

Shuffle<T>(IEnumerable<T>)

Shuffles the elements of the collection using Fisher-Yates algorithm.

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

Parameters

source IEnumerable<T>

Returns

IEnumerable<T>

Type Parameters

T

UpdateFromAsync<TAdd, TRemove>(IEnumerable<TRemove>, IEnumerable<TAdd>, IEnumerable<TRemove>, Func<TAdd, CancellationToken, ValueTask>, Func<TRemove, CancellationToken, ValueTask>, CancellationToken)

Asynchronously updates the output collection by adding and removing specified items. This is a Layer 1 (core) method that executes actions on explicit item lists.

public static ValueTask UpdateFromAsync<TAdd, TRemove>(this IEnumerable<TRemove> output, IEnumerable<TAdd> addedItems, IEnumerable<TRemove> removedItems, Func<TAdd, CancellationToken, ValueTask> addAction, Func<TRemove, CancellationToken, ValueTask> removeAction, CancellationToken cancellationToken = default)

Parameters

output IEnumerable<TRemove>

The collection to update.

addedItems IEnumerable<TAdd>

The items to add to the collection.

removedItems IEnumerable<TRemove>

The items to remove from the collection.

addAction Func<TAdd, CancellationToken, ValueTask>

Async action to perform when adding an item. This parameter is required.

removeAction Func<TRemove, CancellationToken, ValueTask>

Async action to perform when removing an item. This parameter is required.

cancellationToken CancellationToken

Token to monitor for cancellation requests.

Returns

ValueTask

Type Parameters

TAdd

The type of items to add.

TRemove

The type of items to remove.

UpdateFromAsync<TInput, TOutput>(IEnumerable<TOutput>, IEnumerable<TInput>, Func<TInput, TOutput, bool>, Func<TInput, CancellationToken, ValueTask>, Func<TOutput, CancellationToken, ValueTask>, CancellationToken)

Asynchronously updates the output collection from the input collection with item comparison and actions for adding and removing items. This is a Layer 2 (diff-calculating) method that calculates differences and delegates to Layer 1.

public static ValueTask UpdateFromAsync<TInput, TOutput>(this IEnumerable<TOutput> output, IEnumerable<TInput> input, Func<TInput, TOutput, bool> areRepresentingTheSameItem, Func<TInput, CancellationToken, ValueTask> addAction, Func<TOutput, CancellationToken, ValueTask> removeAction, CancellationToken cancellationToken = default)

Parameters

output IEnumerable<TOutput>

The collection to update.

input IEnumerable<TInput>

The collection to use as source for updates.

areRepresentingTheSameItem Func<TInput, TOutput, bool>

Function to determine if an input item and output item represent the same entity.

addAction Func<TInput, CancellationToken, ValueTask>

Async action to perform when adding an item.

removeAction Func<TOutput, CancellationToken, ValueTask>

Async action to perform when removing an item.

cancellationToken CancellationToken

Token to monitor for cancellation requests.

Returns

ValueTask

Type Parameters

TInput

The type of the input items.

TOutput

The type of the output items.

UpdateFromAsync<TInput, TOutput>(IEnumerable<TOutput>, IEnumerable<TInput>, Func<TInput, TOutput, bool>, Func<TInput, TOutput, bool>, Func<TInput, CancellationToken, ValueTask>, Func<TOutput, TInput, CancellationToken, ValueTask>, Func<TOutput, CancellationToken, ValueTask>, CancellationToken)

Asynchronously updates the output collection from the input collection with item comparison and actions for adding, updating, and removing items. This is a Layer 2 (diff-calculating) method that calculates differences and delegates to Layer 1.

public static ValueTask UpdateFromAsync<TInput, TOutput>(this IEnumerable<TOutput> output, IEnumerable<TInput> input, Func<TInput, TOutput, bool> areRepresentingTheSameItem, Func<TInput, TOutput, bool> areRepresentingTheSameValue, Func<TInput, CancellationToken, ValueTask> addAction, Func<TOutput, TInput, CancellationToken, ValueTask> updateAction, Func<TOutput, CancellationToken, ValueTask> removeAction, CancellationToken cancellationToken = default)

Parameters

output IEnumerable<TOutput>

The collection to update.

input IEnumerable<TInput>

The collection to use as source for updates.

areRepresentingTheSameItem Func<TInput, TOutput, bool>

Function to determine if an input item and output item represent the same entity.

areRepresentingTheSameValue Func<TInput, TOutput, bool>

Function to determine if an input item and output item have the same value.

addAction Func<TInput, CancellationToken, ValueTask>

Async action to perform when adding an item.

updateAction Func<TOutput, TInput, CancellationToken, ValueTask>

Async action to perform when updating an item.

removeAction Func<TOutput, CancellationToken, ValueTask>

Async action to perform when removing an item.

cancellationToken CancellationToken

Token to monitor for cancellation requests.

Returns

ValueTask

Type Parameters

TInput

The type of the input items.

TOutput

The type of the output items.

UpdateFromAsync<TAdd, TUpdate, TRemove>(IEnumerable<TRemove>, IEnumerable<TAdd>, IEnumerable<TUpdate>, IEnumerable<TRemove>, Func<TAdd, CancellationToken, ValueTask>, Func<TUpdate, CancellationToken, ValueTask>, Func<TRemove, CancellationToken, ValueTask>, CancellationToken)

Asynchronously updates the output collection by adding, updating, and removing specified items. This is a Layer 1 (core) method that executes actions on explicit item lists.

public static ValueTask UpdateFromAsync<TAdd, TUpdate, TRemove>(this IEnumerable<TRemove> output, IEnumerable<TAdd> addedItems, IEnumerable<TUpdate> updatedItems, IEnumerable<TRemove> removedItems, Func<TAdd, CancellationToken, ValueTask> addAction, Func<TUpdate, CancellationToken, ValueTask> updateAction, Func<TRemove, CancellationToken, ValueTask> removeAction, CancellationToken cancellationToken = default)

Parameters

output IEnumerable<TRemove>

The collection to update.

addedItems IEnumerable<TAdd>

The items to add to the collection.

updatedItems IEnumerable<TUpdate>

The items to update in the collection.

removedItems IEnumerable<TRemove>

The items to remove from the collection.

addAction Func<TAdd, CancellationToken, ValueTask>

Async action to perform when adding an item. This parameter is required.

updateAction Func<TUpdate, CancellationToken, ValueTask>

Async action to perform when updating an item. This parameter is required.

removeAction Func<TRemove, CancellationToken, ValueTask>

Async action to perform when removing an item. This parameter is required.

cancellationToken CancellationToken

Token to monitor for cancellation requests.

Returns

ValueTask

Type Parameters

TAdd

The type of items to add.

TUpdate

The type of items to update.

TRemove

The type of items to remove.

UpdateFrom<TAdd, TRemove>(IEnumerable<TRemove>, IEnumerable<TAdd>, IEnumerable<TRemove>, Action<TAdd>, Action<TRemove>)

Updates the output collection by adding and removing specified items. This is a Layer 1 (core) method that executes actions on explicit item lists.

public static void UpdateFrom<TAdd, TRemove>(this IEnumerable<TRemove> output, IEnumerable<TAdd> addedItems, IEnumerable<TRemove> removedItems, Action<TAdd> addAction, Action<TRemove> removeAction)

Parameters

output IEnumerable<TRemove>

The collection to update.

addedItems IEnumerable<TAdd>

The items to add to the collection.

removedItems IEnumerable<TRemove>

The items to remove from the collection.

addAction Action<TAdd>

Action to perform when adding an item. This parameter is required.

removeAction Action<TRemove>

Action to perform when removing an item. This parameter is required.

Type Parameters

TAdd

The type of items to add.

TRemove

The type of items to remove.

UpdateFrom<TInput, TOutput>(IEnumerable<TOutput>, IEnumerable<TInput>, Func<TInput, TOutput, bool>, Action<TInput>, Action<TOutput>)

Updates the output collection from the input collection with item comparison and actions for adding and removing items. This is a Layer 2 (diff-calculating) method that calculates differences and delegates to Layer 1.

public static void UpdateFrom<TInput, TOutput>(this IEnumerable<TOutput> output, IEnumerable<TInput> input, Func<TInput, TOutput, bool> areRepresentingTheSameItem, Action<TInput> addAction, Action<TOutput> removeAction)

Parameters

output IEnumerable<TOutput>

The collection to update.

input IEnumerable<TInput>

The collection to use as source for updates.

areRepresentingTheSameItem Func<TInput, TOutput, bool>

Function to determine if an input item and output item represent the same entity.

addAction Action<TInput>

Action to perform when adding an item.

removeAction Action<TOutput>

Action to perform when removing an item.

Type Parameters

TInput

The type of the input items.

TOutput

The type of the output items.

UpdateFrom<TInput, TOutput>(IEnumerable<TOutput>, IEnumerable<TInput>, Func<TInput, TOutput, bool>, Func<TInput, TOutput, bool>, Action<TInput>, Action<TOutput, TInput>, Action<TOutput>)

Updates the output collection from the input collection with item comparison and actions for adding, updating, and removing items. This is a Layer 2 (diff-calculating) method that calculates differences and delegates to Layer 1.

public static void UpdateFrom<TInput, TOutput>(this IEnumerable<TOutput> output, IEnumerable<TInput> input, Func<TInput, TOutput, bool> areRepresentingTheSameItem, Func<TInput, TOutput, bool> areRepresentingTheSameValue, Action<TInput> addAction, Action<TOutput, TInput> updateAction, Action<TOutput> removeAction)

Parameters

output IEnumerable<TOutput>

The collection to update.

input IEnumerable<TInput>

The collection to use as source for updates.

areRepresentingTheSameItem Func<TInput, TOutput, bool>

Function to determine if an input item and output item represent the same entity.

areRepresentingTheSameValue Func<TInput, TOutput, bool>

Function to determine if an input item and output item have the same value.

addAction Action<TInput>

Action to perform when adding an item.

updateAction Action<TOutput, TInput>

Action to perform when updating an item.

removeAction Action<TOutput>

Action to perform when removing an item.

Type Parameters

TInput

The type of the input items.

TOutput

The type of the output items.

UpdateFrom<TInput, TOutput>(IEnumerable<TOutput>, IEnumerable<TInput>, Func<TInput, TOutput, bool>, Func<TInput, TOutput>, Action<TOutput>, Action<TOutput>)

Updates the output collection from the input collection with item comparison, type conversion, and actions for adding and removing items. This is a Layer 2 (diff-calculating) method that calculates differences, converts types, and delegates to Layer 1.

public static void UpdateFrom<TInput, TOutput>(this IEnumerable<TOutput> output, IEnumerable<TInput> input, Func<TInput, TOutput, bool> areRepresentingTheSameItem, Func<TInput, TOutput> fromInputTypeToOutputTypeConversion, Action<TOutput> addAction, Action<TOutput> removeAction)

Parameters

output IEnumerable<TOutput>

The collection to update.

input IEnumerable<TInput>

The collection to use as source for updates.

areRepresentingTheSameItem Func<TInput, TOutput, bool>

Function to determine if an input item and output item represent the same entity.

fromInputTypeToOutputTypeConversion Func<TInput, TOutput>

Function to convert input items to output items.

addAction Action<TOutput>

Action to perform when adding an item.

removeAction Action<TOutput>

Action to perform when removing an item.

Type Parameters

TInput

The type of the input items.

TOutput

The type of the output items.

UpdateFrom<TAdd, TUpdate, TRemove>(IEnumerable<TRemove>, IEnumerable<TAdd>, IEnumerable<TUpdate>, IEnumerable<TRemove>, Action<TAdd>, Action<TUpdate>, Action<TRemove>)

Updates the output collection by adding, updating, and removing specified items. This is a Layer 1 (core) method that executes actions on explicit item lists.

public static void UpdateFrom<TAdd, TUpdate, TRemove>(this IEnumerable<TRemove> output, IEnumerable<TAdd> addedItems, IEnumerable<TUpdate> updatedItems, IEnumerable<TRemove> removedItems, Action<TAdd> addAction, Action<TUpdate> updateAction, Action<TRemove> removeAction)

Parameters

output IEnumerable<TRemove>

The collection to update.

addedItems IEnumerable<TAdd>

The items to add to the collection.

updatedItems IEnumerable<TUpdate>

The items to update in the collection.

removedItems IEnumerable<TRemove>

The items to remove from the collection.

addAction Action<TAdd>

Action to perform when adding an item. This parameter is required.

updateAction Action<TUpdate>

Action to perform when updating an item. This parameter is required.

removeAction Action<TRemove>

Action to perform when removing an item. This parameter is required.

Type Parameters

TAdd

The type of items to add.

TUpdate

The type of items to update.

TRemove

The type of items to remove.