C# List CopyTo(Int32, T[], Int32, Int32)

Description

List CopyTo(Int32, T[], Int32, Int32) copies a range of elements from the List to a compatible one-dimensional array, starting at the specified index of the target array.

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 List at 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);
   }
}




















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




HashSet
LinkedList
LinkedListNode
List
Queue
SortedSet
Stack