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.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
) 作業,其中 n
是 Count。