StringDictionary.CopyTo(Array, Int32) 方法

定义

将字符串字典值复制到一维 Array 实例的指定索引位置。

public:
 virtual void CopyTo(Array ^ array, int index);
public virtual void CopyTo (Array array, int index);
abstract member CopyTo : Array * int -> unit
override this.CopyTo : Array * int -> unit
Public Overridable Sub CopyTo (array As Array, index As Integer)

参数

array
Array

一维 Array,为从 StringDictionary 所复制的值的目标位置。

index
Int32

array 中复制开始处的索引。

例外

array 是多维的。

- 或 -

StringDictionary 中的元素个数大于 indexarray 末尾之间的可用空间。

arraynull

index 小于 array 的下限。

示例

下面的代码示例演示如何将 StringDictionary 复制到数组。

#using <System.dll>

using namespace System;
using namespace System::Collections;
using namespace System::Collections::Specialized;
void main()
{
   
   // Creates and initializes a new StringDictionary.
   StringDictionary^ myCol = gcnew StringDictionary;
   myCol->Add( "red", "rojo" );
   myCol->Add( "green", "verde" );
   myCol->Add( "blue", "azul" );
   
   // Displays the values in the StringDictionary.
   Console::WriteLine( "KEYS\tVALUES in the StringDictionary" );
   IEnumerator^ myEnum = myCol->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      DictionaryEntry^ myDE = safe_cast<DictionaryEntry^>(myEnum->Current);
      Console::WriteLine( "{0}\t{1}", myDE->Key, myDE->Value );
      Console::WriteLine();
      
      // Creates an array with DictionaryEntry elements.
      array<DictionaryEntry>^myArr = gcnew array<DictionaryEntry>(3);
      
      // Copies the StringDictionary to the array.
      myCol->CopyTo( myArr, 0 );
      
      // Displays the values in the array.
      Console::WriteLine( "KEYS\tVALUES in the array" );
      for ( int i = 0; i < myArr->Length; i++ )
         Console::WriteLine( "{0}\t{1}", myArr[ i ].Key, myArr[ i ].Value );
      Console::WriteLine();
   }
}

/*
This code produces the following output.

KEYS    VALUES in the StringDictionary
green   verde
red     rojo
blue    azul

KEYS    VALUES in the array
green   verde
red     rojo
blue    azul

*/
using System;
using System.Collections;
using System.Collections.Specialized;

public class SamplesStringDictionary  {

   public static void Main()  {

      // Creates and initializes a new StringDictionary.
      StringDictionary myCol = new StringDictionary();
      myCol.Add( "red", "rojo" );
      myCol.Add( "green", "verde" );
      myCol.Add( "blue", "azul" );

      // Displays the values in the StringDictionary.
      Console.WriteLine( "KEYS\tVALUES in the StringDictionary" );
      foreach ( DictionaryEntry myDE in myCol )
         Console.WriteLine( "{0}\t{1}", myDE.Key, myDE.Value );
      Console.WriteLine();

      // Creates an array with DictionaryEntry elements.
      DictionaryEntry[] myArr = { new DictionaryEntry(), new DictionaryEntry(), new DictionaryEntry() };

      // Copies the StringDictionary to the array.
      myCol.CopyTo( myArr, 0 );

      // Displays the values in the array.
      Console.WriteLine( "KEYS\tVALUES in the array" );
      for ( int i = 0; i < myArr.Length; i++ )
         Console.WriteLine( "{0}\t{1}", myArr[i].Key, myArr[i].Value );
      Console.WriteLine();
   }
}

/*
This code produces the following output.

KEYS    VALUES in the StringDictionary
green   verde
red     rojo
blue    azul

KEYS    VALUES in the array
green   verde
red     rojo
blue    azul

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

Public Class SamplesStringDictionary

   Public Shared Sub Main()

      ' Creates and initializes a new StringDictionary.
      Dim myCol As New StringDictionary()
      myCol.Add("red", "rojo")
      myCol.Add("green", "verde")
      myCol.Add("blue", "azul")

      ' Displays the values in the StringDictionary.
      Console.WriteLine("KEYS" + ControlChars.Tab + "VALUES in the StringDictionary")
      Dim myDE As DictionaryEntry
      For Each myDE In  myCol
         Console.WriteLine("{0}" + ControlChars.Tab + "{1}", myDE.Key, myDE.Value)
      Next myDE
      Console.WriteLine()

      ' Creates an array with DictionaryEntry elements.
      Dim myArr As DictionaryEntry() =  {New DictionaryEntry(), New DictionaryEntry(), New DictionaryEntry()}

      ' Copies the StringDictionary to the array.
      myCol.CopyTo(myArr, 0)

      ' Displays the values in the array.
      Console.WriteLine("KEYS" + ControlChars.Tab + "VALUES in the array")
      Dim i As Integer
      For i = 0 To myArr.Length - 1
         Console.WriteLine("{0}" + ControlChars.Tab + "{1}", myArr(i).Key, myArr(i).Value)
      Next i
      Console.WriteLine()

   End Sub

End Class


'This code produces the following output.
'
'KEYS    VALUES in the StringDictionary
'green   verde
'red     rojo
'blue    azul
'
'KEYS    VALUES in the array
'green   verde
'red     rojo
'blue    azul

注解

CopyTo 将可以类型转换 System.Collections.DictionaryEntry的对象复制到 。 DictionaryEntry 同时包含 键和 值。

复制到 的 Array 元素的排序顺序与枚举器循环访问 的顺序 StringDictionary相同。

此方法是 O (n) 操作,其中 nCount

适用于