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부터 시작하는 인덱스입니다.

예외

array이(가) null인 경우

index가 0보다 작은 경우

array가 다차원 배열인 경우

또는

소스 StringCollection의 요소 수가 대상 arrayindex부터 끝까지 사용 가능한 공간보다 큽니다.

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

예제

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

#using <System.dll>

using namespace System;
using namespace System::Collections;
using namespace System::Collections::Specialized;
void PrintValues( IEnumerable^ myCol );
int main()
{
   
   // Creates and initializes a new StringCollection.
   StringCollection^ myCol = gcnew StringCollection;
   array<String^>^myArr = {"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.
   array<String^>^myArr2 = gcnew array<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();
}

void PrintValues( IEnumerable^ myCol )
{
   IEnumerator^ myEnum = myCol->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      Object^ obj = safe_cast<Object^>(myEnum->Current);
      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

*/
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합니다.

적용 대상

추가 정보