Extensions
StringExtensions
SourceProvides extension methods for strings.
SplitOnWhiteSpace
string[] SplitOnWhiteSpace(this string? input)
Splits a string on any whitespace character, removing empty entries.
Parameters
-
inputThe string to split.
Returns
An array of substrings delimited by whitespace.
EnumerableExtensions
Source
Provides extension methods for IEnumerable{T}
to perform parallel operations.
ParallelSelectAsync
3 overloads
Task<IEnumerable<TResult>> ParallelSelectAsync<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, CancellationToken, Task<TResult>> body, int maxDegreeOfParallelism, CancellationToken cancellationToken = default)
Projects each element of a sequence into a new form with bounded concurrency and preserves the input order of results.
Parameters
-
sourceThe sequence of input items. -
bodyAn asynchronous transform that receives an input item and a cancellation token and produces a result. -
maxDegreeOfParallelismThe maximum number of concurrent invocations ofbody; must be greater than zero. -
cancellationTokenA token to observe while waiting for tasks to complete.
Returns
An IEnumerable{T}
of results in the same order as the input sequence.
Exceptions
-
ArgumentNullExceptionThrown whensourceorbodyis null. -
ArgumentOutOfRangeExceptionThrown whenmaxDegreeOfParallelismis less than or equal to zero. -
OperationCanceledExceptionThe operation was canceled viacancellationToken.
Remarks
Exceptions thrown by the body
function propagate to the returned task.
Task<IEnumerable<TResult>> ParallelSelectAsync<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, CancellationToken, Task<TResult>> body, int maxDegreeOfParallelism, CancellationToken cancellationToken, ILogger logger)
Projects each element of a sequence into a new form with bounded concurrency, preserves the input order of results, observes cancellationToken
, and logs suppressed cleanup diagnostics through logger
.
Parameters
-
sourceThe sequence of input items. -
bodyAn asynchronous transform that receives an input item and a cancellation token and produces a result. -
maxDegreeOfParallelismThe maximum number of concurrent invocations ofbody; must be greater than zero. -
cancellationTokenA token to observe while waiting for tasks to complete. -
loggerLogger for cleanup diagnostics that would otherwise be suppressed to preserve the primary exception.
Returns
An IEnumerable{T}
of results in the same order as the input sequence.
Exceptions
-
ArgumentNullExceptionThrown whensource,body, orloggeris null. -
ArgumentOutOfRangeExceptionThrown whenmaxDegreeOfParallelismis less than or equal to zero. -
OperationCanceledExceptionThe operation was canceled viacancellationToken.
Remarks
Exceptions thrown by the body
function propagate to the returned task.
IAsyncEnumerable<TResult> ParallelSelectAsync<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, Task<TResult>> body, int maxDegreeOfParallelism, CancellationToken cancellationToken = default)
Produces an async sequence of results projected from each element of source
, yielding results in the original input order while limiting concurrent selector executions.
Parameters
-
sourceThe input sequence to project. -
bodyA selector that produces aTask{TResult}for an input element; this selector does not receive or observe the suppliedcancellationToken. -
maxDegreeOfParallelismThe maximum number of selector tasks allowed to run concurrently; must be greater than zero. -
cancellationTokenA token to observe for request to cancel the overall enumeration.
Returns
The sequence of transformed elements in the original input order; concurrency is limited to maxDegreeOfParallelism
.
ParallelSelectAsyncEnumerable
3 overloads
IAsyncEnumerable<TResult> ParallelSelectAsyncEnumerable<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, CancellationToken, Task<TResult>> body, int maxDegreeOfParallelism, int bufferMultiplier = 4, CancellationToken cancellationToken = default)
Produces an asynchronous sequence of results by applying body
to each element of source
, limiting concurrency to maxDegreeOfParallelism
and preserving the input order. The internal channel capacity is capped at maxDegreeOfParallelism * bufferMultiplier
.
Parameters
-
sourceThe input sequence to project; must not be null. -
bodyA selector that projects an element to aTask{TResult}; it receives the element and aCancellationTokenthat is signaled when the operation is canceled. -
maxDegreeOfParallelismThe maximum number of selector tasks that may run concurrently; must be greater than zero. -
bufferMultiplierA multiplier used to compute the internal channel capacity asmaxDegreeOfParallelism * bufferMultiplier(capped toint.MaxValue); must be at least 1. -
cancellationTokenToken to observe for cooperative cancellation of the overall operation.
Returns
An IAsyncEnumerable{TResult}
that yields projected results in the same order as the source sequence.
Exceptions
-
ArgumentNullExceptionThrown ifsourceorbodyis null. -
ArgumentOutOfRangeExceptionThrown ifmaxDegreeOfParallelismis less than or equal to zero or ifbufferMultiplieris less than 1.
IAsyncEnumerable<TResult> ParallelSelectAsyncEnumerable<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, CancellationToken, Task<TResult>> body, int maxDegreeOfParallelism, int bufferMultiplier, CancellationToken cancellationToken, ILogger logger)
Produces an asynchronous sequence of results by applying body
to each element of source
, limiting concurrency, preserving input order, observing cancellationToken
, and logging suppressed cleanup diagnostics through logger
.
Parameters
-
sourceThe input sequence to project; must not be null. -
bodyA selector that projects an element to aTask{TResult}; it receives the element and a cancellation token that is signaled when the operation is canceled. -
maxDegreeOfParallelismThe maximum number of selector tasks that may run concurrently; must be greater than zero. -
bufferMultiplierA multiplier used to compute the internal channel capacity asmaxDegreeOfParallelism * bufferMultiplier(capped toint.MaxValue); must be at least 1. -
cancellationTokenToken to observe for cooperative cancellation of the overall operation. -
loggerLogger for cleanup diagnostics that would otherwise be suppressed to preserve the primary exception.
Returns
An IAsyncEnumerable{TResult}
that yields projected results in the same order as the source sequence.
Exceptions
-
ArgumentNullExceptionThrown ifsource,body, orloggeris null. -
ArgumentOutOfRangeExceptionThrown ifmaxDegreeOfParallelismis less than or equal to zero or ifbufferMultiplieris less than 1.
IAsyncEnumerable<TResult> ParallelSelectAsyncEnumerable<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, Task<TResult>> body, int maxDegreeOfParallelism, int bufferMultiplier = 4, CancellationToken cancellationToken = default)
Projects each element of source
using the provided task-returning selector with bounded concurrency and yields the results in the same order as the source.
Parameters
-
sourceThe input sequence to project. -
bodyA selector that produces aTask{TResult}for an input element. This overload does not pass or observe aCancellationTokento the selector. -
maxDegreeOfParallelismThe maximum number of selector tasks allowed to run concurrently; must be greater than zero. -
bufferMultiplierMultiplier applied tomaxDegreeOfParallelismto determine internal channel capacity; must be at least 1. -
cancellationTokenToken to cancel iteration and background work.
Returns
An IAsyncEnumerable{TResult}
that yields projected results in the input order.
Exceptions
-
ArgumentNullExceptionsourceorbodyis null. -
ArgumentOutOfRangeExceptionmaxDegreeOfParallelismis less than or equal to 0, orbufferMultiplieris less than 1.