Method Do
| Edit this page View SourceDo<TSource>(IEnumerable<TSource>, Action<TSource>)
Lazily invokes an action for each value in the sequence.
Declaration
public static IEnumerable<TSource> Do<TSource>(this IEnumerable<TSource> source, Action<TSource> onNext)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<TSource> | source | Source sequence. |
Action<TSource> | onNext | Action to invoke for each element. |
Returns
Type | Description |
---|---|
IEnumerable<TSource> | Sequence exhibiting the specified side-effects upon enumeration. |
Type Parameters
Name | Description |
---|---|
TSource | Source sequence element type. |
Remarks
This method uses deferred execution and streams its results.
Examples
The following code example demonstrates how to lazily invoke actions for each element using Do
.
var sequence = Enumerable.Range(1, 5);
// Execute an action for each element
var result = sequence
.Do(i => Console.Write($"{i}, "));
Console.WriteLine("Before");
result.Consume();
Console.WriteLine("After");
// This code produces the following output:
// Before
// 1, 2, 3, 4, 5, After
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
Do<TSource>(IEnumerable<TSource>, Action<TSource>, Action)
Lazily invokes an action for each value in the sequence, and executes an action for successful termination.
Declaration
public static IEnumerable<TSource> Do<TSource>(this IEnumerable<TSource> source, Action<TSource> onNext, Action onCompleted)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<TSource> | source | Source sequence. |
Action<TSource> | onNext | Action to invoke for each element. |
Action | onCompleted | Action to invoke on successful termination of the sequence. |
Returns
Type | Description |
---|---|
IEnumerable<TSource> | Sequence exhibiting the specified side-effects upon enumeration. |
Type Parameters
Name | Description |
---|---|
TSource | Source sequence element type. |
Remarks
This method uses deferred execution and streams its results.
Examples
The following code example demonstrates how to lazily invoke actions for each element using Do
.
var sequence = Enumerable.Range(1, 5);
// Execute an action for each element, and on completion
var result = sequence
.Do(
i => Console.Write($"{i}, "),
() => Console.WriteLine("Completed"));
Console.WriteLine("Before");
result.Consume();
Console.WriteLine("After");
// This code produces the following output:
// Before
// 1, 2, 3, 4, 5, Completed
// After
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
Do<TSource>(IEnumerable<TSource>, Action<TSource>, Action<Exception>)
Lazily invokes an action for each value in the sequence, and executes an action upon exceptional termination.
Declaration
public static IEnumerable<TSource> Do<TSource>(this IEnumerable<TSource> source, Action<TSource> onNext, Action<Exception> onError)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<TSource> | source | Source sequence. |
Action<TSource> | onNext | Action to invoke for each element. |
Action<Exception> | onError | Action to invoke on exceptional termination of the sequence. |
Returns
Type | Description |
---|---|
IEnumerable<TSource> | Sequence exhibiting the specified side-effects upon enumeration. |
Type Parameters
Name | Description |
---|---|
TSource | Source sequence element type. |
Remarks
The exception is caught and passed to onNext
, and then it is re-thrown. Appropriate
error-handling is still required in order to safely consume the sequence.
This method uses deferred execution and streams its results.
Examples
The following code example demonstrates how to lazily invoke actions for each element using Do
.
var sequence = Enumerable.Range(1, 4).Concat(SuperEnumerable.Throw<int>(new InvalidOperationException()));
// Execute an action for each element, on error, and on completion
var result = sequence
.Do(
i => Console.Write($"{i}, "),
ex => Console.WriteLine("Failed: " + ex.Message));
Console.WriteLine("Before");
try
{
result.Consume();
}
catch (InvalidOperationException) {}
Console.WriteLine("After");
// This code produces the following output:
// Before
// 1, 2, 3, 4, Failed: Operation is not valid due to the current state of the object.
// After
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
Do<TSource>(IEnumerable<TSource>, Action<TSource>, Action<Exception>, Action)
Lazily invokes an action for each value in the sequence, and executes an action upon successful or exceptional termination.
Declaration
public static IEnumerable<TSource> Do<TSource>(this IEnumerable<TSource> source, Action<TSource> onNext, Action<Exception> onError, Action onCompleted)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<TSource> | source | Source sequence. |
Action<TSource> | onNext | Action to invoke for each element. |
Action<Exception> | onError | Action to invoke on exceptional termination of the sequence. |
Action | onCompleted | Action to invoke on successful termination of the sequence. |
Returns
Type | Description |
---|---|
IEnumerable<TSource> | Sequence exhibiting the specified side-effects upon enumeration. |
Type Parameters
Name | Description |
---|---|
TSource | Source sequence element type. |
Remarks
The exception is caught and passed to onNext
, and then it is re-thrown. Appropriate
error-handling is still required in order to safely consume the sequence.
This method uses deferred execution and streams its results.
Examples
The following code example demonstrates how to lazily invoke actions for each element using Do
.
var flag = false;
var sequence = SuperEnumerable.If(
() => flag,
Enumerable.Range(1, 5),
Enumerable.Range(1, 4).Concat(SuperEnumerable.Throw<int>(new InvalidOperationException())));
// Execute an action for each element, on error, and on completion
var result = sequence
.Do(
i => Console.Write($"{i}, "),
ex => Console.WriteLine("Failed: " + ex.Message),
() => Console.WriteLine("Completed"));
Console.WriteLine("Before 1");
try
{
result.Consume();
}
catch (InvalidOperationException) {}
Console.WriteLine("After 1");
flag = true;
Console.WriteLine("Before 2");
result.Consume();
Console.WriteLine("After 2");
// This code produces the following output:
// Before 1
// 1, 2, 3, 4, Failed: Operation is not valid due to the current state of the object.
// After 1
// Before 2
// 1, 2, 3, 4, 5, Completed
// After 2
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|