Method Replace
| Edit this page View SourceReplace<TSource>(IEnumerable<TSource>, int, TSource)
Replaces a single value in a sequence at a specified index with the given replacement value.
Declaration
public static IEnumerable<TSource> Replace<TSource>(this IEnumerable<TSource> source, int index, TSource value)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<TSource> | source | The source sequence. |
int | index | The index of the value to replace. |
TSource | value | The replacement value to use at |
Returns
Type | Description |
---|---|
IEnumerable<TSource> | A sequence with the original values from |
Type Parameters
Name | Description |
---|---|
TSource | Type of item in the sequence |
Remarks
This operator evaluates in a deferred and streaming manner.
Examples
The following code example demonstrates how to replace a value using Replace
.
var sequence = Enumerable.Range(1, 10);
// Replace a value in a sequence
var result = sequence
.Replace(3, -100);
Console.WriteLine(
"[" +
string.Join(", ", result) +
"]");
// This code produces the following output:
// [1, 2, 3, -100, 5, 6, 7, 8, 9, 10]
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
Replace<TSource>(IEnumerable<TSource>, Index, TSource)
Replaces a single value in a sequence at a specified index with the given replacement value.
Declaration
public static IEnumerable<TSource> Replace<TSource>(this IEnumerable<TSource> source, Index index, TSource value)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<TSource> | source | The source sequence. |
Index | index | The index of the value to replace. |
TSource | value | The replacement value to use at |
Returns
Type | Description |
---|---|
IEnumerable<TSource> | A sequence with the original values from |
Type Parameters
Name | Description |
---|---|
TSource | Type of item in the sequence |
Remarks
This operator evaluates in a deferred and streaming manner.
Examples
The following code example demonstrates how to replace a value using Replace
.
var sequence = Enumerable.Range(1, 10);
// Replace a value in a sequence
var result = sequence
.Replace(^3, -100);
Console.WriteLine(
"[" +
string.Join(", ", result) +
"]");
// This code produces the following output:
// [1, 2, 3, 4, 5, 6, 7, -100, 9, 10]
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|