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
queueConcurrentQueue<T>The queue to enqueue the item to. Cannot be null.
objTThe item to enqueue. Can be null.
maxintThe 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
enumerableIEnumerable<T>The enumerable to get the element from. Can be null.
indexintThe index of the element to get. If the index is out of range or if the enumerable is null, the default value is returned.
defaultValueTThe 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
inputIEnumerableThe 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
inputIEnumerable<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
sourceIEnumerable<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
sourceIEnumerable<T>countint
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
sourceIEnumerable<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
outputIEnumerable<TOutput>inputIEnumerable<TInput>areRepresentingTheSameItemFunc<TInput, TOutput, bool>addActionAction<TInput>removeActionAction<TOutput>
Type Parameters
TInputTOutput