C# Collection CopyTo

Description

copies the entire Collection to a compatible one-dimensional Array, starting at the specified index of the target array.

Syntax


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

Parameters

  • array - The one-dimensional Array that is the destination of the elements copied from Collection . The Array must have zero-based indexing.
  • index - The zero-based index in array at which copying begins.

Example


//from w  w  w  .  j  av a2s  .  co  m
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;

public class Demo
{
    public static void Main()
    {
        Collection<string> myData = new Collection<string>();

        myData.Add("A");
        myData.Add("B");
        myData.Add("C");
        myData.Add("D");
        
        string[] s = new string[10];
        
        myData.CopyTo(s,0);
    }
}




















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




Collection