Class Collection
Provides utility methods to work with collections.
Inherited Members
Namespace: DotNext.Collections.Generic
Assembly: DotNext.dll
Syntax
public static class Collection
Methods
| Edit this page View SourceAddAll<T>(ICollection<T>, IEnumerable<T>)
Adds multiple items into collection.
Declaration
public static void AddAll<T>(this ICollection<T> collection, IEnumerable<T> items)
Parameters
Type | Name | Description |
---|---|---|
ICollection<T> | collection | A collection to modify. |
IEnumerable<T> | items | An items to add into collection. |
Type Parameters
Name | Description |
---|---|
T | The type of elements in the collection. |
Append<T>(IEnumerable<T>, params T[])
Adds items
to the end of collection
.
Declaration
public static IEnumerable<T> Append<T>(this IEnumerable<T> collection, params T[] items)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | collection | The collection to be concatenated with the items. |
T[] | items | The items to be added to the end of the collection. |
Returns
Type | Description |
---|---|
IEnumerable<T> | The concatenated collection. |
Type Parameters
Name | Description |
---|---|
T | The type of items in the collection. |
Convert<TInput, TOutput>(IReadOnlyCollection<TInput>, Converter<TInput, TOutput>)
Returns lazily converted read-only collection.
Declaration
public static ReadOnlyCollectionView<TInput, TOutput> Convert<TInput, TOutput>(this IReadOnlyCollection<TInput> collection, Converter<TInput, TOutput> converter)
Parameters
Type | Name | Description |
---|---|---|
IReadOnlyCollection<TInput> | collection | Read-only collection to convert. |
Converter<TInput, TOutput> | converter | A collection item conversion function. |
Returns
Type | Description |
---|---|
ReadOnlyCollectionView<TInput, TOutput> | Lazily converted read-only collection. |
Type Parameters
Name | Description |
---|---|
TInput | Type of items in the source collection. |
TOutput | Type of items in the target collection. |
Copy<T>(IEnumerable<T>, int, MemoryAllocator<T>?)
Creates a copy of the elements from the collection.
Declaration
public static MemoryOwner<T> Copy<T>(this IEnumerable<T> enumerable, int sizeHint = 0, MemoryAllocator<T>? allocator = null)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | enumerable | The collection to copy. |
int | sizeHint | The approximate size of the collection, if known. |
MemoryAllocator<T> | allocator | The allocator of the memory block used to place copied elements. |
Returns
Type | Description |
---|---|
MemoryOwner<T> | Rented memory block containing a copy of the elements from the collection. |
Type Parameters
Name | Description |
---|---|
T | The type of elements in the collection. |
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
ArgumentOutOfRangeException |
|
ElementAt<T>(IEnumerable<T>, int, out T)
Obtains element at the specified index in the sequence.
Declaration
public static bool ElementAt<T>(this IEnumerable<T> collection, int index, out T element)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | collection | Source collection. |
int | index | Index of the element to read. |
T | element | Obtained element. |
Returns
Type | Description |
---|---|
bool | true, if element is available in the collection and obtained successfully; otherwise, false. |
Type Parameters
Name | Description |
---|---|
T | Type of elements in the sequence. |
Remarks
This method is optimized for types IList<T> and IReadOnlyList<T>.
FirstOrNone<T>(IEnumerable<T>)
Obtains the first element of a sequence; or None if the sequence is empty.
Declaration
public static Optional<T> FirstOrNone<T>(this IEnumerable<T> collection)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | collection | The collection to return the first element of. |
Returns
Type | Description |
---|---|
Optional<T> | The first element; or None |
Type Parameters
Name | Description |
---|---|
T | The type of the element of a collection. |
ForEachAsync<T>(IEnumerable<T>, Func<T, CancellationToken, ValueTask>, CancellationToken)
Applies the specified asynchronous action to each collection element.
Declaration
public static ValueTask ForEachAsync<T>(this IEnumerable<T> collection, Func<T, CancellationToken, ValueTask> action, CancellationToken token = default)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | collection | A collection to enumerate. Cannot be null. |
Func<T, CancellationToken, ValueTask> | action | An action to applied for each element. |
CancellationToken | token | The token that can be used to cancel the enumeration. |
Returns
Type | Description |
---|---|
ValueTask | The task representing asynchronous execution of this method. |
Type Parameters
Name | Description |
---|---|
T | Type of elements in the collection. |
Exceptions
Type | Condition |
---|---|
OperationCanceledException | The enumeration has been canceled. |
ForEach<T>(IEnumerable<T>, Action<T>)
Applies specified action to each collection element.
Declaration
public static void ForEach<T>(this IEnumerable<T> collection, Action<T> action)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | collection | A collection to enumerate. Cannot be null. |
Action<T> | action | An action to applied for each element. |
Type Parameters
Name | Description |
---|---|
T | Type of elements in the collection. |
GetConsumer<T>(IProducerConsumerCollection<T>)
Gets consumer of thread-safe concurrent collection.
Declaration
public static Collection.ConsumingEnumerable<T> GetConsumer<T>(this IProducerConsumerCollection<T> collection)
Parameters
Type | Name | Description |
---|---|---|
IProducerConsumerCollection<T> | collection | The concurrent collection. |
Returns
Type | Description |
---|---|
Collection.ConsumingEnumerable<T> | The consumer in the form of enumerable collection. |
Type Parameters
Name | Description |
---|---|
T | The type of elements in the collection. |
LastOrNone<T>(IEnumerable<T>)
Obtains the last element of a sequence; or None if the sequence is empty.
Declaration
public static Optional<T> LastOrNone<T>(this IEnumerable<T> collection)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | collection | The collection to return the first element of. |
Returns
Type | Description |
---|---|
Optional<T> | The first element; or None |
Type Parameters
Name | Description |
---|---|
T | The type of the element of a collection. |
Prepend<T>(IEnumerable<T>, params T[])
Adds items
to the beginning of collection
.
Declaration
public static IEnumerable<T> Prepend<T>(this IEnumerable<T> collection, params T[] items)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | collection | The collection to be concatenated with the items. |
T[] | items | The items to be added to the beginning of the collection. |
Returns
Type | Description |
---|---|
IEnumerable<T> | The concatenated collection. |
Type Parameters
Name | Description |
---|---|
T | The type of items in the collection. |
SequenceHashCode<T>(IEnumerable<T>, bool)
Computes hash code for the sequence of objects.
Declaration
public static int SequenceHashCode<T>(this IEnumerable<T> sequence, bool salted = true)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | sequence | The sequence of elements. |
bool | salted | true to include randomized salt data into hashing; false to use data from memory only. |
Returns
Type | Description |
---|---|
int | The hash code computed from each element in the sequence. |
Type Parameters
Name | Description |
---|---|
T | Type of the elements in the sequence. |
SkipNulls<T>(IEnumerable<T?>)
Skip null values in the collection.
Declaration
public static IEnumerable<T> SkipNulls<T>(this IEnumerable<T?> collection) where T : class
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | collection | A collection to check. Cannot be null. |
Returns
Type | Description |
---|---|
IEnumerable<T> | Modified lazy collection without null values. |
Type Parameters
Name | Description |
---|---|
T | Type of elements in the collection. |
ToArray<T>(ICollection<T>)
Converts collection into single-dimensional array.
Declaration
public static T[] ToArray<T>(ICollection<T> collection)
Parameters
Type | Name | Description |
---|---|---|
ICollection<T> | collection | A collection to convert. |
Returns
Type | Description |
---|---|
T[] | Array of collection items. |
Type Parameters
Name | Description |
---|---|
T | Type of collection items. |
ToArray<T>(IReadOnlyCollection<T>)
Converts read-only collection into single-dimensional array.
Declaration
public static T[] ToArray<T>(IReadOnlyCollection<T> collection)
Parameters
Type | Name | Description |
---|---|---|
IReadOnlyCollection<T> | collection | A collection to convert. |
Returns
Type | Description |
---|---|
T[] | Array of collection items. |
Type Parameters
Name | Description |
---|---|
T | Type of collection items. |
ToAsyncEnumerable<T>(IEnumerable<T>)
Converts synchronous collection of elements to asynchronous.
Declaration
public static IAsyncEnumerable<T> ToAsyncEnumerable<T>(this IEnumerable<T> enumerable)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | enumerable | The collection of elements. |
Returns
Type | Description |
---|---|
IAsyncEnumerable<T> | The asynchronous wrapper over synchronous collection of elements. |
Type Parameters
Name | Description |
---|---|
T | The type of the elements in the collection. |
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
ToAsyncEnumerator<TEnumerator, T>(TEnumerator, CancellationToken)
Converts ad-hoc enumerator to a generic enumerator.
Declaration
public static IAsyncEnumerator<T> ToAsyncEnumerator<TEnumerator, T>(this TEnumerator enumerator, CancellationToken token) where TEnumerator : struct, IEnumerator<TEnumerator, T>
Parameters
Type | Name | Description |
---|---|---|
TEnumerator | enumerator | Ad-hoc enumerator. |
CancellationToken | token | The token that can be used to cancel the enumeration. |
Returns
Type | Description |
---|---|
IAsyncEnumerator<T> | The enumerator over values of type |
Type Parameters
Name | Description |
---|---|
TEnumerator | |
T |
ToClassicEnumerator<TEnumerator, T>(TEnumerator)
Converts ad-hoc enumerator to a generic enumerator.
Declaration
public static IEnumerator<T> ToClassicEnumerator<TEnumerator, T>(this TEnumerator enumerator) where TEnumerator : struct, IEnumerator<TEnumerator, T>
Parameters
Type | Name | Description |
---|---|---|
TEnumerator | enumerator | Ad-hoc enumerator. |
Returns
Type | Description |
---|---|
IEnumerator<T> | The enumerator over values of type |
Type Parameters
Name | Description |
---|---|
TEnumerator | |
T |
ToString<T>(IEnumerable<T>, string, string)
Concatenates each element from the collection into single string.
Declaration
public static string ToString<T>(this IEnumerable<T> collection, string delimiter, string ifEmpty = "")
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | collection | Collection to convert. Cannot be null. |
string | delimiter | Delimiter between elements in the final string. |
string | ifEmpty | A string to be returned if collection has no elements. |
Returns
Type | Description |
---|---|
string | Converted collection into string. |
Type Parameters
Name | Description |
---|---|
T | Type of array elements. |