Class Sequence
Various methods to work with classes implementing IEnumerable<T> interface.
Inherited Members
Namespace: DotNext.Collections.Generic
Assembly: DotNext.dll
Syntax
public static class Sequence
Methods
| Improve this Doc View SourceAppend<T>(IEnumerable<T>, 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. |
ElementAt<T>(IEnumerable<T>, Int32, 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. |
Int32 | index | Index of the element to read. |
T | element | Obtained element. |
Returns
Type | Description |
---|---|
Boolean | 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>.
ElementAtAsync<T>(IAsyncEnumerable<T>, Int32, CancellationToken)
Obtains element at the specified index in the sequence.
Declaration
public static ValueTask<Optional<T>> ElementAtAsync<T>(this IAsyncEnumerable<T> collection, int index, CancellationToken token = default(CancellationToken))
Parameters
Type | Name | Description |
---|---|---|
IAsyncEnumerable<T> | collection | Source collection. |
Int32 | index | Index of the element to read. |
CancellationToken | token | The token that can be used to cancel enumeration. |
Returns
Type | Description |
---|---|
ValueTask<Optional<T>> | The requested element; or None if index is out of range. |
Type Parameters
Name | Description |
---|---|
T | Type of elements in the sequence. |
Exceptions
Type | Condition |
---|---|
OperationCanceledException | The operation has been canceled. |
FirstOrEmpty<T>(IEnumerable<T>)
Obtains first value in the sequence; or None if sequence is empty.
Declaration
public static Optional<T> FirstOrEmpty<T>(this IEnumerable<T> seq)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | seq | A sequence to check. Cannot be null. |
Returns
Type | Description |
---|---|
Optional<T> | The first element in the sequence; or None if sequence is empty. |
Type Parameters
Name | Description |
---|---|
T | Type of elements in the sequence. |
FirstOrEmpty<T>(IEnumerable<T>, ValueFunc<T, Boolean>)
Returns the first element in a sequence that satisfies a specified condition.
Declaration
public static Optional<T> FirstOrEmpty<T>(this IEnumerable<T> seq, in ValueFunc<T, bool> filter)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | seq | A collection to return an element from. |
ValueFunc<T, Boolean> | filter | A function to test each element for a condition. |
Returns
Type | Description |
---|---|
Optional<T> | The first element in the sequence that matches to the specified filter; or empty value. |
Type Parameters
Name | Description |
---|---|
T | The type of the elements of source. |
FirstOrEmpty<T>(IEnumerable<T>, Predicate<T>)
Returns the first element in a sequence that satisfies a specified condition.
Declaration
public static Optional<T> FirstOrEmpty<T>(this IEnumerable<T> seq, Predicate<T> filter)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | seq | A collection to return an element from. |
Predicate<T> | filter | A function to test each element for a condition. |
Returns
Type | Description |
---|---|
Optional<T> | The first element in the sequence that matches to the specified filter; or empty value. |
Type Parameters
Name | Description |
---|---|
T | The type of the elements of source. |
FirstOrEmptyAsync<T>(IAsyncEnumerable<T>, ValueFunc<T, Boolean>, CancellationToken)
Returns the first element in a sequence that satisfies a specified condition.
Declaration
public static ValueTask<Optional<T>> FirstOrEmptyAsync<T>(this IAsyncEnumerable<T> seq, ValueFunc<T, bool> filter, CancellationToken token = default(CancellationToken))
Parameters
Type | Name | Description |
---|---|---|
IAsyncEnumerable<T> | seq | A collection to return an element from. |
ValueFunc<T, Boolean> | filter | A function to test each element for a condition. |
CancellationToken | token | The token that can be used to cancel enumeration. |
Returns
Type | Description |
---|---|
ValueTask<Optional<T>> | The first element in the sequence that matches to the specified filter; or empty value. |
Type Parameters
Name | Description |
---|---|
T | The type of the elements of source. |
Exceptions
Type | Condition |
---|---|
OperationCanceledException | The operation has been canceled. |
FirstOrEmptyAsync<T>(IAsyncEnumerable<T>, Predicate<T>, CancellationToken)
Returns the first element in a sequence that satisfies a specified condition.
Declaration
public static ValueTask<Optional<T>> FirstOrEmptyAsync<T>(this IAsyncEnumerable<T> seq, Predicate<T> filter, CancellationToken token = default(CancellationToken))
Parameters
Type | Name | Description |
---|---|---|
IAsyncEnumerable<T> | seq | A collection to return an element from. |
Predicate<T> | filter | A function to test each element for a condition. |
CancellationToken | token | The token that can be used to cancel enumeration. |
Returns
Type | Description |
---|---|
ValueTask<Optional<T>> | The first element in the sequence that matches to the specified filter; or empty value. |
Type Parameters
Name | Description |
---|---|
T | The type of the elements of source. |
Exceptions
Type | Condition |
---|---|
OperationCanceledException | The operation has been canceled. |
FirstOrEmptyAsync<T>(IAsyncEnumerable<T>, CancellationToken)
Obtains first value in the sequence; or None if sequence is empty.
Declaration
public static ValueTask<Optional<T>> FirstOrEmptyAsync<T>(this IAsyncEnumerable<T> seq, CancellationToken token = default(CancellationToken))
Parameters
Type | Name | Description |
---|---|---|
IAsyncEnumerable<T> | seq | A sequence to check. Cannot be null. |
CancellationToken | token | The token that can be used to cancel enumeration. |
Returns
Type | Description |
---|---|
ValueTask<Optional<T>> | The first element in the sequence; or None if sequence is empty. |
Type Parameters
Name | Description |
---|---|
T | Type of elements in the sequence. |
Exceptions
Type | Condition |
---|---|
OperationCanceledException | The operation has been canceled. |
FirstOrNull<T>(IEnumerable<T>)
Obtains first value type in the sequence; or null if sequence is empty.
Declaration
public static T? FirstOrNull<T>(this IEnumerable<T> seq)
where T : struct
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | seq | A sequence to check. Cannot be null. |
Returns
Type | Description |
---|---|
Nullable<T> | First element in the sequence; or null if sequence is empty. |
Type Parameters
Name | Description |
---|---|
T | Type of elements in the sequence. |
FirstOrNullAsync<T>(IAsyncEnumerable<T>, CancellationToken)
Obtains first value type in the sequence; or null if sequence is empty.
Declaration
public static ValueTask<T?> FirstOrNullAsync<T>(this IAsyncEnumerable<T> seq, CancellationToken token = default(CancellationToken))
where T : struct
Parameters
Type | Name | Description |
---|---|---|
IAsyncEnumerable<T> | seq | A sequence to check. Cannot be null. |
CancellationToken | token | The token that can be used to cancel enumeration. |
Returns
Type | Description |
---|---|
ValueTask<Nullable<T>> | First element in the sequence; or null if sequence is empty. |
Type Parameters
Name | Description |
---|---|
T | Type of elements in the sequence. |
Exceptions
Type | Condition |
---|---|
OperationCanceledException | The operation has been canceled. |
ForEach<T>(IEnumerable<T>, ValueAction<T>)
Applies specified action to each collection element.
Declaration
public static void ForEach<T>(this IEnumerable<T> collection, in ValueAction<T> action)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | collection | A collection to enumerate. Cannot be null. |
ValueAction<T> | action | An action to applied for each element. |
Type Parameters
Name | Description |
---|---|
T | Type of elements in the collection. |
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. |
ForEachAsync<T>(IAsyncEnumerable<T>, ValueAction<T>, CancellationToken)
Applies specified action to each collection element asynchronously.
Declaration
public static ValueTask ForEachAsync<T>(this IAsyncEnumerable<T> collection, ValueAction<T> action, CancellationToken token = default(CancellationToken))
Parameters
Type | Name | Description |
---|---|---|
IAsyncEnumerable<T> | collection | A collection to enumerate. Cannot be null. |
ValueAction<T> | 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. |
ForEachAsync<T>(IAsyncEnumerable<T>, ValueFunc<T, CancellationToken, ValueTask>, CancellationToken)
Applies specified action to each collection element asynchronously.
Declaration
public static ValueTask ForEachAsync<T>(this IAsyncEnumerable<T> collection, ValueFunc<T, CancellationToken, ValueTask> action, CancellationToken token = default(CancellationToken))
Parameters
Type | Name | Description |
---|---|---|
IAsyncEnumerable<T> | collection | A collection to enumerate. Cannot be null. |
ValueFunc<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. |
ForEachAsync<T>(IAsyncEnumerable<T>, Action<T>, CancellationToken)
Applies specified action to each collection element asynchronously.
Declaration
public static ValueTask ForEachAsync<T>(this IAsyncEnumerable<T> collection, Action<T> action, CancellationToken token = default(CancellationToken))
Parameters
Type | Name | Description |
---|---|---|
IAsyncEnumerable<T> | collection | A collection to enumerate. Cannot be null. |
Action<T> | 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. |
ForEachAsync<T>(IAsyncEnumerable<T>, Func<T, CancellationToken, ValueTask>, CancellationToken)
Applies specified action to each collection element asynchronously.
Declaration
public static ValueTask ForEachAsync<T>(this IAsyncEnumerable<T> collection, Func<T, CancellationToken, ValueTask> action, CancellationToken token = default(CancellationToken))
Parameters
Type | Name | Description |
---|---|---|
IAsyncEnumerable<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. |
ForEachAsync<T>(IEnumerable<T>, ValueFunc<T, CancellationToken, ValueTask>, CancellationToken)
Applies the specified asynchronous action to each collection element.
Declaration
public static ValueTask ForEachAsync<T>(this IEnumerable<T> collection, ValueFunc<T, CancellationToken, ValueTask> action, CancellationToken token = default(CancellationToken))
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | collection | A collection to enumerate. Cannot be null. |
ValueFunc<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. |
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(CancellationToken))
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. |
GetAsyncEnumerator<T>(IEnumerable<T>, CancellationToken)
Obtains asynchronous enumerator over the sequence of elements.
Declaration
public static IAsyncEnumerator<T> GetAsyncEnumerator<T>(this IEnumerable<T> enumerable, CancellationToken token = default(CancellationToken))
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | enumerable | The collection of elements. |
CancellationToken | token | The token that can be used by consumer to cancel the enumeration. |
Returns
Type | Description |
---|---|
IAsyncEnumerator<T> | The asynchronous wrapper over synchronous enumerator. |
Type Parameters
Name | Description |
---|---|
T | The type of the elements in the collection. |
GetConsumer<T>(IProducerConsumerCollection<T>)
Gets consumer of thread-safe concurrent collection.
Declaration
public static Sequence.ConsumingEnumerable<T> GetConsumer<T>(this IProducerConsumerCollection<T> collection)
Parameters
Type | Name | Description |
---|---|---|
IProducerConsumerCollection<T> | collection | The concurrent collection. |
Returns
Type | Description |
---|---|
Sequence.ConsumingEnumerable<T> | The consumer in the form of enumerable collection. |
Type Parameters
Name | Description |
---|---|
T | The type of elements in the collection. |
Limit<T>(IEnumerator<T>, Int32, Boolean)
Limits the number of the elements in the sequence.
Declaration
public static Sequence.LimitedEnumerator<T> Limit<T>(this IEnumerator<T> enumerator, int count, bool leaveOpen = false)
Parameters
Type | Name | Description |
---|---|---|
IEnumerator<T> | enumerator | The sequence of the elements. |
Int32 | count | The maximum number of the elements in the returned sequence. |
Boolean | leaveOpen | false to dispose |
Returns
Type | Description |
---|---|
Sequence.LimitedEnumerator<T> | The enumerator which is limited by count. |
Type Parameters
Name | Description |
---|---|
T | The type of items in the sequence. |
Prepend<T>(IEnumerable<T>, 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(IEnumerable<Object>, Boolean)
Computes hash code for the sequence of objects.
Declaration
public static int SequenceHashCode(this IEnumerable<object> sequence, bool salted = true)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<Object> | sequence | The sequence of elements. |
Boolean | salted | true to include randomized salt data into hashing; false to use data from memory only. |
Returns
Type | Description |
---|---|
Int32 | The hash code computed from each element in the sequence. |
Singleton<T>(T)
Constructs a sequence from the single element.
Declaration
public static IEnumerable<T> Singleton<T>(T item)
Parameters
Type | Name | Description |
---|---|---|
T | item | An item to be placed into sequence. |
Returns
Type | Description |
---|---|
IEnumerable<T> | Sequence of single element. |
Type Parameters
Name | Description |
---|---|
T | Type of element. |
Skip<T>(IEnumerator<T>, Int32)
Bypasses a specified number of elements in a sequence.
Declaration
public static bool Skip<T>(this IEnumerator<T> enumerator, int count)
Parameters
Type | Name | Description |
---|---|---|
IEnumerator<T> | enumerator | Enumerator to modify. Cannot be null. |
Int32 | count | The number of elements to skip. |
Returns
Type | Description |
---|---|
Boolean | true, if current element is available; otherwise, false. |
Type Parameters
Name | Description |
---|---|
T | The type of the elements in the sequence. |
Skip<TEnumerator, T>(ref TEnumerator, Int32)
Bypasses a specified number of elements in a sequence.
Declaration
public static bool Skip<TEnumerator, T>(this ref TEnumerator enumerator, int count)
where TEnumerator : struct, IEnumerator<T>
Parameters
Type | Name | Description |
---|---|---|
TEnumerator | enumerator | Enumerator to modify. |
Int32 | count | The number of elements to skip. |
Returns
Type | Description |
---|---|
Boolean | true, if current element is available; otherwise, false. |
Type Parameters
Name | Description |
---|---|
TEnumerator | The type of the sequence. |
T | The type of the elements in the sequence. |
SkipAsync<T>(IAsyncEnumerator<T>, Int32)
Bypasses a specified number of elements in a sequence.
Declaration
public static ValueTask<bool> SkipAsync<T>(this IAsyncEnumerator<T> enumerator, int count)
Parameters
Type | Name | Description |
---|---|---|
IAsyncEnumerator<T> | enumerator | Enumerator to modify. Cannot be null. |
Int32 | count | The number of elements to skip. |
Returns
Type | Description |
---|---|
ValueTask<Boolean> | true, if current element is available; otherwise, false. |
Type Parameters
Name | Description |
---|---|
T | The type of the elements in the sequence. |
Exceptions
Type | Condition |
---|---|
OperationCanceledException | The operation has been canceled. |
SkipNulls<T>(IAsyncEnumerable<T>)
Skip null values in the collection.
Declaration
public static IAsyncEnumerable<T> SkipNulls<T>(this IAsyncEnumerable<T> collection)
where T : class
Parameters
Type | Name | Description |
---|---|---|
IAsyncEnumerable<T> | collection | A collection to check. Cannot be null. |
Returns
Type | Description |
---|---|
IAsyncEnumerable<T> | Modified lazy collection without null values. |
Type Parameters
Name | Description |
---|---|
T | Type of elements in the collection. |
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. |
ToArrayAsync<T>(IAsyncEnumerable<T>, Int32, MemoryAllocator<T>, CancellationToken)
Converts asynchronous collection to the array.
Declaration
public static Task<T[]> ToArrayAsync<T>(this IAsyncEnumerable<T> collection, int initialCapacity = 10, MemoryAllocator<T> allocator = null, CancellationToken token = default(CancellationToken))
Parameters
Type | Name | Description |
---|---|---|
IAsyncEnumerable<T> | collection | The asynchronous collection. |
Int32 | initialCapacity | The initial capacity of internal buffer. |
MemoryAllocator<T> | allocator | The memory allocator used by internal buffer. |
CancellationToken | token | The token that can be used to cancel the operation. |
Returns
Type | Description |
---|---|
Task<T[]> | The array representing all elements from the source collection. |
Type Parameters
Name | Description |
---|---|
T | Type of elements in the collection. |
Exceptions
Type | Condition |
---|---|
OperationCanceledException | The operation has been canceled. |
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. |
ToAsyncEnumerable<T>(Func<CancellationToken, ValueTask<Optional<T>>>)
Converts generator function to enumerable collection.
Declaration
public static Sequence.AsyncGenerator<T> ToAsyncEnumerable<T>(this Func<CancellationToken, ValueTask<Optional<T>>> generator)
Parameters
Type | Name | Description |
---|---|---|
Func<CancellationToken, ValueTask<Optional<T>>> | generator | Stateful generator function. |
Returns
Type | Description |
---|---|
Sequence.AsyncGenerator<T> | The enumerable collection. |
Type Parameters
Name | Description |
---|---|
T | The type of the elements in the collection. |
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
ToEnumerable<T>(Func<Optional<T>>)
Converts generator function to enumerable collection.
Declaration
public static Sequence.Generator<T> ToEnumerable<T>(this Func<Optional<T>> generator)
Parameters
Type | Name | Description |
---|---|---|
Func<Optional<T>> | generator | Stateful generator function. |
Returns
Type | Description |
---|---|
Sequence.Generator<T> | The enumerable collection. |
Type Parameters
Name | Description |
---|---|
T | The type of the elements in the collection. |
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
ToEnumerator<T>(ReadOnlyMemory<T>)
Gets enumerator over all elements in the memory.
Declaration
public static IEnumerator<T> ToEnumerator<T>(ReadOnlyMemory<T> memory)
Parameters
Type | Name | Description |
---|---|---|
ReadOnlyMemory<T> | memory | The memory block to be converted. |
Returns
Type | Description |
---|---|
IEnumerator<T> | The enumerator over all elements in the memory. |
Type Parameters
Name | Description |
---|---|
T | The type of elements in the memory. |
See Also
| Improve this Doc View SourceToString<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. |