Hashtable.CopyTo(Array, Int32) Method is used to copy the elements of a Hashtable to a one-dimensional Array instance at the specified index.
public virtual void CopyTo (Array array, int arrayIndex);
array : The one-dimensional Array that is the destination of the DictionaryEntry objects copied from Hashtable. The Array must have zero-based indexing.
index : The zero-based index in array at which copying begins.
- ArgumentNullException : If the array is null.
- ArgumentOutOfRangeException : If the index is less than zero.
- InvalidCastException : If the type of the source Hashtable cannot be cast automatically to the type of the destination array.
- ArgumentException : If array is multidimensional OR the number of elements in the source Hashtable is greater than the available space from arrayIndex to the end of the destination array.
Below programs illustrate the use of Hashtable.CopyTo(Array, Int32) Method:
B --> Banana C --> Cat A --> Apple F --> Fish D --> Dog E --> Elephant
System.ArgumentOutOfRangeException: Non-negative number required.
Parameter name: arrayIndex
- The elements are copied to the Array in the same order in which the enumerator iterates through the Hashtable.
- To copy only the keys in the Hashtable, use Hashtable.Keys.CopyTo.
- To copy only the values in the Hashtable, use Hashtable.Values.CopyTo.
- This method is an O(n) operation, where n is Count.
- C# | Copying the HybridDictionary entries to an Array Instance
- C# | Copying the elements of ArrayList to a new array
- C# | Copying the Collection<T> elements to an array
- C# | Copying BitArray elements to an Array
- Copying the Queue elements to 1-D Array in C#
- C# | Copying the SortedList elements to an Array Object
- C# | Copy OrderedDictionary elements to Array instance at the specified index
- C# | Remove all elements from the Hashtable
- C# | Copying the entire ArrayList to 1-D Array starting at the specified index
- C# | Copy ListDictionary to Array instance at the specified index
- C# | Check if a Hashtable is equal to another Hashtable
- C# | Getting the type of the current instance
- C# | Get or Set the value associated with specified key in Hashtable
- C# Hashtable with Examples
- C# | Hashtable Class
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.