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

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.

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>
input IEnumerable<TInput>
areRepresentingTheSameItem Func<TInput, TOutput, bool>
addAction Action<TInput>
removeAction Action<TOutput>

Type Parameters

TInput
TOutput