StringCollection.CopyTo(String[], Int32) 메서드

정의

대상 배열의 지정된 인덱스에서 시작하여 전체 StringCollection 값을 문자열의 1차원 배열에 복사합니다.

public:
 void CopyTo(cli::array <System::String ^> ^ array, int index);
public void CopyTo(string[] array, int index);
member this.CopyTo : string[] * int -> unit
Public Sub CopyTo (array As String(), index As Integer)

매개 변수

array
String[]

복사 StringCollection된 요소의 대상인 문자열의 1차원 배열입니다. Array 0부터 시작하는 인덱싱이 있어야 합니다.

index
Int32

복사가 시작되는 인덱 array 스(0부터 시작)입니다.

예외

arraynull입니다.

index가 0보다 작습니다.

array 가 다차원입니다.

-또는-

원본 StringCollection 의 요소 수가 대상index의 끝까지 사용 가능한 공간 array 보다 큽합니다.

원본 StringCollection 의 형식을 대상 array의 형식으로 자동으로 캐스팅할 수 없습니다.

예제

다음 코드 예제에서는 배열에 StringCollection 복사합니다.

using System;
using System.Collections;
using System.Collections.Specialized;

public class SamplesStringCollection  {

   public static void Main()  {

      // Creates and initializes a new StringCollection.
      StringCollection myCol = new StringCollection();
      String[] myArr = new String[] { "RED", "orange", "yellow", "RED", "green", "blue", "RED", "indigo", "violet", "RED" };
      myCol.AddRange( myArr );

      Console.WriteLine( "Initial contents of the StringCollection:" );
      PrintValues( myCol );

      // Copies the collection to a new array starting at index 0.
      String[] myArr2 = new String[myCol.Count];
      myCol.CopyTo( myArr2, 0 );

      Console.WriteLine( "The new array contains:" );
      for ( int i = 0; i < myArr2.Length; i++ )  {
         Console.WriteLine( "   [{0}] {1}", i, myArr2[i] );
      }
      Console.WriteLine();
   }

   public static void PrintValues( IEnumerable myCol )  {
      foreach ( Object obj in myCol )
         Console.WriteLine( "   {0}", obj );
      Console.WriteLine();
   }
}

/*
This code produces the following output.

Initial contents of the StringCollection:
   RED
   orange
   yellow
   RED
   green
   blue
   RED
   indigo
   violet
   RED

The new array contains:
   [0] RED
   [1] orange
   [2] yellow
   [3] RED
   [4] green
   [5] blue
   [6] RED
   [7] indigo
   [8] violet
   [9] RED

*/
Imports System.Collections
Imports System.Collections.Specialized

Public Class SamplesStringCollection   

   Public Shared Sub Main()

      ' Creates and initializes a new StringCollection.
      Dim myCol As New StringCollection()
      Dim myArr() As [String] = {"RED", "orange", "yellow", "RED", "green", "blue", "RED", "indigo", "violet", "RED"}
      myCol.AddRange(myArr)

      Console.WriteLine("Initial contents of the StringCollection:")
      PrintValues(myCol)

      ' Copies the collection to a new array starting at index 0.
      Dim myArr2(myCol.Count) As [String]
      myCol.CopyTo(myArr2, 0)

      Console.WriteLine("The new array contains:")
      Dim i As Integer
      For i = 0 To myArr2.Length - 1
         Console.WriteLine("   [{0}] {1}", i, myArr2(i))
      Next i
      Console.WriteLine()

   End Sub

   Public Shared Sub PrintValues(myCol As IEnumerable)
      Dim obj As [Object]
      For Each obj In  myCol
         Console.WriteLine("   {0}", obj)
      Next obj
      Console.WriteLine()
   End Sub

End Class


'This code produces the following output.
'
'Initial contents of the StringCollection:
'   RED
'   orange
'   yellow
'   RED
'   green
'   blue
'   RED
'   indigo
'   violet
'   RED
'
'The new array contains:
'   [0] RED
'   [1] orange
'   [2] yellow
'   [3] RED
'   [4] green
'   [5] blue
'   [6] RED
'   [7] indigo
'   [8] violet
'   [9] RED
'

설명

지정된 배열은 호환되는 형식이어야 합니다.

요소는 열거자가 StringCollection 반복하는 순서와 동일한 순서로 복사 Array 됩니다StringCollection.

이 메서드는 O(n) 연산입니다. 여기서 nCount.

적용 대상

추가 정보