StringCollection.CopyTo(String[], Int32) メソッド

定義

StringCollection値全体を、ターゲット配列の指定したインデックスから始まる文字列の 1 次元配列にコピーします。

public:
 void CopyTo(cli::array <System::String ^> ^ array, int index);
public void CopyTo(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
'

注釈

指定した配列は互換性のある型である必要があります。

要素は、Arrayの列挙子がStringCollectionを反復処理するのと同じ順序でStringCollectionにコピーされます。

このメソッドは O(n) 操作であり、 nCount

適用対象

こちらもご覧ください