Method Pad
| Edit this page View SourcePad<TSource>(IEnumerable<TSource>, int)
Pads a sequence with default values if it is narrower (shorter in length) than a given width.
Declaration
public static IEnumerable<TSource?> Pad<TSource>(this IEnumerable<TSource> source, int width)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<TSource> | source | The sequence to pad. |
int | width | The width/length below which to pad. |
Returns
Type | Description |
---|---|
IEnumerable<TSource> | Returns a sequence that is at least as wide/long as the width/length specified by the |
Type Parameters
Name | Description |
---|---|
TSource | The type of the elements of |
Remarks
This operator uses deferred execution and streams its results.
Examples
The following code example demonstrates how to pad a sequence using Pad
.
var sequence = Enumerable.Range(1, 3);
// Pad a sequence until it is at least a certain length
var result = sequence
.Pad(6);
Console.WriteLine(
"[" +
string.Join(", ", result) +
"]");
// This code produces the following output:
// [1, 2, 3, 0, 0, 0]
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
ArgumentOutOfRangeException |
|
Pad<TSource>(IEnumerable<TSource>, int, TSource)
Pads a sequence with default values if it is narrower (shorter in length) than a given width.
Declaration
public static IEnumerable<TSource> Pad<TSource>(this IEnumerable<TSource> source, int width, TSource padding)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<TSource> | source | The sequence to pad. |
int | width | The width/length below which to pad. |
TSource | padding | The value to use for padding. |
Returns
Type | Description |
---|---|
IEnumerable<TSource> | Returns a sequence that is at least as wide/long as the width/length specified by the |
Type Parameters
Name | Description |
---|---|
TSource | The type of the elements of |
Remarks
This operator uses deferred execution and streams its results.
Examples
The following code example demonstrates how to pad a sequence using Pad
.
var sequence = Enumerable.Range(1, 3);
// Pad a sequence until it is at least a certain length
var result = sequence
.Pad(6, -5);
Console.WriteLine(
"[" +
string.Join(", ", result) +
"]");
// This code produces the following output:
// [1, 2, 3, -5, -5, -5]
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
ArgumentOutOfRangeException |
|
Pad<TSource>(IEnumerable<TSource>, int, Func<int, TSource>)
Pads a sequence with default values if it is narrower (shorter in length) than a given width.
Declaration
public static IEnumerable<TSource> Pad<TSource>(this IEnumerable<TSource> source, int width, Func<int, TSource> paddingSelector)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<TSource> | source | The sequence to pad. |
int | width | The width/length below which to pad. |
Func<int, TSource> | paddingSelector | A function to generate the value used as padding. |
Returns
Type | Description |
---|---|
IEnumerable<TSource> | Returns a sequence that is at least as wide/long as the width/length specified by the |
Type Parameters
Name | Description |
---|---|
TSource | The type of the elements of |
Remarks
This operator uses deferred execution and streams its results.
Examples
The following code example demonstrates how to pad a sequence using Pad
.
var sequence = Enumerable.Range(1, 3);
// Pad a sequence until it is at least a certain length
var result = sequence
.Pad(6, i => -i);
Console.WriteLine(
"[" +
string.Join(", ", result) +
"]");
// This code produces the following output:
// [1, 2, 3, -3, -4, -5]
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
ArgumentOutOfRangeException |
|