Method CopyTo
| Edit this page View SourceCopyTo<TSource>(IEnumerable<TSource>, Span<TSource>)
Copies the contents of a sequence into a provided span.
Declaration
public static int CopyTo<TSource>(this IEnumerable<TSource> source, Span<TSource> span)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<TSource> | source | The source sequence. |
Span<TSource> | span | The span that is the destination of the elements copied from |
Returns
Type | Description |
---|---|
int | The number of elements actually copied. |
Type Parameters
Name | Description |
---|---|
TSource | The type of elements of |
Remarks
All data from source
will be copied to span
if possible.
If source
is shorter than span
, then any remaining elements will be
untouched. If source
is longer than span
, then an exception will be
thrown.
This operator executes immediately.
Examples
The following code example demonstrates how to copy data to a span using the CopyTo
operator.
var sequence = Enumerable.Range(1, 5);
var destination = new int[7];
var span = destination.AsSpan();
// Copy the provided sequence to a span
var result = sequence.CopyTo(span[1..]);
Console.WriteLine(result);
Console.WriteLine(
"[" +
string.Join(", ", destination) +
"]");
// This code produces the following output:
// 5
// [0, 1, 2, 3, 4, 5, 0]
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
ArgumentException |
|
CopyTo<TSource>(IEnumerable<TSource>, TSource[])
Copies the contents of a sequence into a provided span.
Declaration
public static int CopyTo<TSource>(this IEnumerable<TSource> source, TSource[] array)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<TSource> | source | The source sequence. |
TSource[] | array | The span that is the destination of the elements copied from |
Returns
Type | Description |
---|---|
int | The number of elements actually copied. |
Type Parameters
Name | Description |
---|---|
TSource | The type of elements of |
Remarks
All data from source
will be copied to array
if possible.
If source
is shorter than array
, then any remaining elements will be
untouched. If source
is longer than array
, then an exception will be
thrown.
This operator executes immediately.
Examples
The following code example demonstrates how to copy data to an array using the CopyTo
operator.
var sequence = Enumerable.Range(1, 5);
var destination = new int[5];
// Copy the provided sequence to an array
var result = sequence.CopyTo(destination);
Console.WriteLine(result);
Console.WriteLine(
"[" +
string.Join(", ", destination) +
"]");
// This code produces the following output:
// 5
// [1, 2, 3, 4, 5]
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
ArgumentException |
|
CopyTo<TSource>(IEnumerable<TSource>, IList<TSource>)
Copies the contents of a sequence into a provided list.
Declaration
public static int CopyTo<TSource>(this IEnumerable<TSource> source, IList<TSource> list)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<TSource> | source | The source sequence. |
IList<TSource> | list | The list that is the destination of the elements copied from |
Returns
Type | Description |
---|---|
int | The number of elements actually copied. |
Type Parameters
Name | Description |
---|---|
TSource | The type of elements of |
Remarks
All data from source
will be copied to list
, starting at position 0.
If source
is shorter than list
, then any remaining elements will be
untouched. If source
is longer than list
, then an exception may be
thrown if the list
has a fixed size (an Array, for example).
This operator executes immediately.
Examples
The following code example demonstrates how to copy data to a list using the CopyTo
operator.
var sequence = Enumerable.Range(1, 5);
var destination = new List<int>() { -1, -2, };
// Copy the provided sequence to a list
var result = sequence.CopyTo(destination);
Console.WriteLine(result);
Console.WriteLine(
"[" +
string.Join(", ", destination) +
"]");
// This code produces the following output:
// 5
// [1, 2, 3, 4, 5]
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
NotSupportedException | The |
CopyTo<TSource>(IEnumerable<TSource>, IList<TSource>, int)
Copies the contents of a sequence into a provided list.
Declaration
public static int CopyTo<TSource>(this IEnumerable<TSource> source, IList<TSource> list, int index)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<TSource> | source | The source sequence. |
IList<TSource> | list | The list that is the destination of the elements copied from |
int | index | The position in |
Returns
Type | Description |
---|---|
int | The number of elements actually copied. |
Type Parameters
Name | Description |
---|---|
TSource | The type of elements of |
Remarks
All data from source
will be copied to list
, starting at position
index
.
If source
is shorter than list
, then any remaining elements will be
untouched. If source
is longer than list
, then an exception may be
thrown if the list
has a fixed size (an Array, for example).
This operator executes immediately.
Examples
The following code example demonstrates how to copy data to a list using the CopyTo
operator.
var sequence = Enumerable.Range(1, 5);
var destination = new List<int>() { -1, -2, };
// Copy the provided sequence to a list at a specified index
var result = sequence.CopyTo(destination, 2);
Console.WriteLine(result);
Console.WriteLine(
"[" +
string.Join(", ", destination) +
"]");
// This code produces the following output:
// 5
// [-1, -2, 1, 2, 3, 4, 5]
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
ArgumentOutOfRangeException |
|
NotSupportedException | The |