C# List CopyTo(T[], Int32)

Description

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

Syntax

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


public void CopyTo(
  T[] array,
  int arrayIndex
)

Parameters

List.CopyTo(T[], Int32) 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.
  • arrayIndex - The zero-based index in array at which copying begins.

Example


//w  w w  .  j  ava 2s.  co  m
using System;
using System.Collections;
using System.Collections.Generic;

class Sample
{
    public static void Main()
    {
        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
        string[] existing = new string[1000];
        words.CopyTo(existing,2);
   }
}




















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




HashSet
LinkedList
LinkedListNode
List
Queue
SortedSet
Stack