The Hashtable class in the System.Collections namespace in C# is used to store key-value pairs in a collection. It provides a CopyTo() method that can be used to copy the elements of the Hashtable to an array.
object[] arr = new object[ht.Count]; ht.CopyTo(arr, 0);
// The elements of the Hashtable are copied to the array 'arr'
In this example, we create a new Hashtable and add some key-value pairs. We then create an object array of the same size as the Hashtable and use the CopyTo() method to copy the elements of the Hashtable to the array.
Example 2:
Hashtable ht = new Hashtable(); ht.Add("Name", "John"); ht.Add("Age", 30); ht.Add("City", "New York");
DictionaryEntry[] arr = new DictionaryEntry[ht.Count]; ht.CopyTo(arr, 0);
// The elements of the Hashtable are copied to the array 'arr' of type DictionaryEntry
In this example, we create a new Hashtable and add some key-value pairs of different types. We then create an array of type DictionaryEntry and use the CopyTo() method to copy the elements of the Hashtable to the array.
This method is part of the .NET Framework Class Library.
C# (CSharp) System.Collections Hashtable.CopyTo - 19 examples found. These are the top rated real world C# (CSharp) examples of System.Collections.Hashtable.CopyTo extracted from open source projects. You can rate examples to help us improve the quality of examples.