C# List CopyTo(T[])

Description

List CopyTo(T[]) copies the entire List to a compatible one-dimensional array, starting at the beginning of the target array.

Syntax

List.CopyTo(T[]) has the following syntax.


public void CopyTo(
  T[] array
)

Parameters

List.CopyTo(T[]) has the following parameters.

  • array - The one-dimensional Array that is the destination of the elements copied from List . The Array must have zero-based indexing.

Example


using System;//w w w  .j a  va  2  s  .c om
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(existing);
   }
}




















Home »
  C# Tutorial »
    System.Collections.Generic »




HashSet
LinkedList
LinkedListNode
List
Queue
SortedSet
Stack