C# Hashtable CopyTo
Description
Hashtable CopyTo
copies the Hashtable elements to a one-dimensional
Array instance at the specified index.
Syntax
Hashtable.CopyTo
has the following syntax.
public virtual void CopyTo(
Array array,
int arrayIndex
)
Parameters
Hashtable.CopyTo
has the following parameters.
array
- The one-dimensional Array that is the destination of the DictionaryEntry objects copied from Hashtable. The Array must have zero-based indexing.arrayIndex
- The zero-based index in array at which copying begins.
Returns
Hashtable.CopyTo
method returns
Example
The following code shows how to
copy the keys from Hashtable
into an array using the
CopyTo()
method and then display the array contents.
using System;/*from w ww .j av a 2 s.com*/
using System.Collections;
class MainClass
{
public static void Main()
{
Hashtable myHashtable = new Hashtable();
myHashtable.Add("AL", "Alabama");
myHashtable.Add("CA", "California");
myHashtable.Add("FL", "Florida");
myHashtable.Add("NY", "New York");
myHashtable.Add("WY", "Wyoming");
foreach (string myKey in myHashtable.Keys)
{
Console.WriteLine("myKey = " + myKey);
}
foreach(string myValue in myHashtable.Values)
{
Console.WriteLine("myValue = " + myValue);
}
Console.WriteLine("Copying keys to myKeys array");
string[] myKeys = new string[5];
myHashtable.Keys.CopyTo(myKeys, 0);
for (int counter = 0; counter < myKeys.Length; counter++) {
Console.WriteLine("myKeys[" + counter + "] = " + myKeys[counter]);
}
}
}
The code above generates the following result.