StringDictionary.CopyTo(Array, Int32) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将字符串字典值复制到指定索引处的一维 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 是 null。
index小于下限。array
示例
下面的代码示例演示如何将 StringDictionary 复制到数组。
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) 操作,其中 n 。Count