C# List CopyTo(Int32, T[], Int32, Int32)
Description
List
copies
a range of elements from the List
Syntax
List.CopyTo(Int32, T[], Int32, Int32)
has the following syntax.
public void CopyTo(
int index,/*from w w w. ja v a2 s . co m*/
T[] array,
int arrayIndex,
int count
)
Parameters
List.CopyTo(Int32, T[], Int32, Int32)
has the following parameters.
index
- The zero-based index in the source Listat which copying begins. array
- The one-dimensional Array that is the destination of the elements copied from List. The Array must have zero-based indexing. arrayIndex
- The zero-based index in array at which copying begins.count
- The number of elements to copy.
Example
using System;/* w w w . j av a2 s . c o m*/
using System.Collections;
using System.Collections.Generic;
class Sample
{
public static void Main()
{
//Create a List of Strings
//String-typed list
List<string> words = new List<string>();
words.Add("A");
words.Add("B");
words.Add("C");
words.Add("D");
words.Add("E");
words.Add("F");
string[] wordsArray = words.ToArray(); // Creates a new typed array
// Copy first two elements to the end of an existing array:
string[] existing = new string[1000];
words.CopyTo(0, existing, 998, 2);
}
}