Class Collection
Provides utility methods to work with collections.
Inherited Members
Namespace: DotNext.Collections.Generic
Assembly: DotNext.dll
Syntax
public static class Collection
Methods
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) where T : notnull
Parameters
| Type | Name | Description |
|---|---|---|
| ICollection<T> | collection | |
| IEnumerable<T> | items | An items to add into collection. |
Type Parameters
| Name | Description |
|---|---|
| T |
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. |
Consume<T>(IProducerConsumerCollection<T>)
Gets consumer of thread-safe concurrent collection.
Declaration
public static Collection.ConsumingEnumerable<T> Consume<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. |
Convert<T, TOutput>(IReadOnlyCollection<T>, Func<T, TOutput>)
Returns lazily converted read-only collection.
Declaration
public static ReadOnlyCollectionView<T, TOutput> Convert<T, TOutput>(this IReadOnlyCollection<T> collection, Func<T, TOutput> converter)
Parameters
| Type | Name | Description |
|---|---|---|
| IReadOnlyCollection<T> | collection | |
| Func<T, TOutput> | converter | A collection item conversion function. |
Returns
| Type | Description |
|---|---|
| ReadOnlyCollectionView<T, TOutput> | Lazily converted read-only collection. |
Type Parameters
| Name | Description |
|---|---|
| T | |
| 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 |
|
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) where T : allows ref struct
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. |
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> col) where T : notnull
Parameters
| Type | Name | Description |
|---|---|---|
| ICollection<T> | col | A collection to convert. |
Returns
| Type | Description |
|---|---|
| T[] | Array of collection items. |
Type Parameters
| Name | Description |
|---|---|
| T |
ToArray<T>(IReadOnlyCollection<T>)
Converts read-only collection into single-dimensional array.
Declaration
public static T[] ToArray<T>(IReadOnlyCollection<T> col) where T : notnull
Parameters
| Type | Name | Description |
|---|---|---|
| IReadOnlyCollection<T> | col | A collection to convert. |
Returns
| Type | Description |
|---|---|
| T[] | Array of collection items. |
Type Parameters
| Name | Description |
|---|---|
| T |
ToAsyncEnumerable<T>(IEnumerable<T>, bool)
Converts the synchronous collection of elements to asynchronous.
Declaration
public static IAsyncEnumerable<T> ToAsyncEnumerable<T>(this IEnumerable<T> enumerable, bool yieldIteration)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<T> | enumerable | The collection of elements. |
| bool | yieldIteration | true to execute every iteration asynchronously; otherwise, false. |
Returns
| Type | Description |
|---|---|
| IAsyncEnumerable<T> | The asynchronous wrapper over the synchronous collection of elements. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of the elements in the collection. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException |
|
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. |