Method FillBackward
| Edit this page View SourceFillBackward<T>(IEnumerable<T?>)
Returns a sequence with each null reference or value in the source replaced with the following non-null reference or value in that sequence.
Declaration
public static IEnumerable<T?> FillBackward<T>(this IEnumerable<T?> source)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | source | The source sequence. |
Returns
Type | Description |
---|---|
IEnumerable<T> | An IEnumerable<T> with null references or values replaced. |
Type Parameters
Name | Description |
---|---|
T | Type of the elements in the source sequence. |
Remarks
This method uses deferred execution semantics and streams its results. If references or values are null at the end of the sequence then they remain null.
Examples
The following code example demonstrates how to fill in missing elements with following elements using FillBackward
.
var sequence = new int?[] { null, null, 1, 2, null, null, null, 3, 4, null, null, };
// Fill in missing elements from later elements
var result = sequence.FillBackward();
Console.WriteLine(
"[" +
string.Join(", ", result) +
"]");
// This code produces the following output:
// [1, 1, 1, 2, 3, 3, 3, 3, 4, , ]
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
FillBackward<T>(IEnumerable<T>, Func<T, bool>)
Returns a sequence with each missing element in the source replaced with the following non-missing element in that sequence. An additional parameter specifies a function used to determine if an element is considered missing or not.
Declaration
public static IEnumerable<T> FillBackward<T>(this IEnumerable<T> source, Func<T, bool> predicate)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | source | The source sequence. |
Func<T, bool> | predicate | The function used to determine if an element in the sequence is considered missing. |
Returns
Type | Description |
---|---|
IEnumerable<T> | An IEnumerable<T> with missing values replaced. |
Type Parameters
Name | Description |
---|---|
T | Type of the elements in the source sequence. |
Remarks
This method uses deferred execution semantics and streams its results. If elements are missing at the end of the sequence then they remain missing.
Examples
The following code example demonstrates how to fill in missing elements with following elements using FillBackward
.
var sequence = Enumerable.Range(1, 10);
// Fill in missing elements from later elements
var result = sequence
.FillBackward(
i => i % 3 < 2);
Console.WriteLine(
"[" +
string.Join(", ", result) +
"]");
// This code produces the following output:
// [2, 2, 5, 5, 5, 8, 8, 8, 9, 10]
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
FillBackward<T>(IEnumerable<T>, Func<T, bool>, Func<T, T, T>)
Returns a sequence with each missing element in the source replaced with the following non-missing element in that sequence. Additional parameters specify two functions, one used to determine if an element is considered missing or not and another to provide the replacement for the missing element.
Declaration
public static IEnumerable<T> FillBackward<T>(this IEnumerable<T> source, Func<T, bool> predicate, Func<T, T, T> fillSelector)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | source | The source sequence. |
Func<T, bool> | predicate | The function used to determine if an element in the sequence is considered missing. |
Func<T, T, T> | fillSelector | The function used to produce the element that will replace the missing one. Its first argument receives the current element considered missing while the second argument receives the next non-missing element. |
Returns
Type | Description |
---|---|
IEnumerable<T> | An IEnumerable<T> with missing elements filled. |
Type Parameters
Name | Description |
---|---|
T | Type of the elements in the source sequence. |
Remarks
This method uses deferred execution semantics and streams its results. If elements are missing at the end of the sequence then they remain missing.
Examples
The following code example demonstrates how to fill in missing elements with following elements using FillBackward
.
var sequence = Enumerable.Range(1, 10);
// Fill in missing elements from later elements
var result = sequence
.FillBackward(
i => i % 3 < 2,
(cur, nxt) => cur * nxt * 100);
Console.WriteLine(
"[" +
string.Join(", ", result) +
"]");
// This code produces the following output:
// [200, 2, 1500, 2000, 5, 4800, 5600, 8, 9, 10]
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|