Method PadStart
| Edit this page View SourcePadStart<TSource>(IEnumerable<TSource>, int)
Pads a sequence with default values in the beginning if it is narrower (shorter in length) than a given width.
Declaration
public static IEnumerable<TSource?> PadStart<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 PadStart
.
var sequence = Enumerable.Range(1, 3);
// Pad a sequence until it is at least a certain length
var result = sequence
.PadStart(6);
Console.WriteLine(
"[" +
string.Join(", ", result) +
"]");
// This code produces the following output:
// [0, 0, 0, 1, 2, 3]
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
ArgumentOutOfRangeException |
|
PadStart<TSource>(IEnumerable<TSource>, int, TSource)
Pads a sequence with default values in the beginning if it is narrower (shorter in length) than a given width.
Declaration
public static IEnumerable<TSource> PadStart<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 PadStart
.
var sequence = Enumerable.Range(1, 3);
// Pad a sequence until it is at least a certain length
var result = sequence
.PadStart(6, -5);
Console.WriteLine(
"[" +
string.Join(", ", result) +
"]");
// This code produces the following output:
// [-5, -5, -5, 1, 2, 3]
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
ArgumentOutOfRangeException |
|
PadStart<TSource>(IEnumerable<TSource>, int, Func<int, TSource>)
Pads a sequence with default values in the beginning if it is narrower (shorter in length) than a given width.
Declaration
public static IEnumerable<TSource> PadStart<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 PadStart
.
var sequence = Enumerable.Range(1, 3);
// Pad a sequence until it is at least a certain length
var result = sequence
.PadStart(6, i => -i);
Console.WriteLine(
"[" +
string.Join(", ", result) +
"]");
// This code produces the following output:
// [0, -1, -2, 1, 2, 3]
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
ArgumentOutOfRangeException |
|