C# | Copying the HybridDictionary entries to an Array Instance
HybridDictionary.CopyTo(Array, Int32) method is used to copy the HybridDictionary entries to a one-dimensional Array instance at the specified index.
Syntax:
public void CopyTo (Array array, int index);
Parameters:
array : The one-dimensional Array that is the destination of the DictionaryEntry objects copied from HybridDictionary. The Array must have zero-based indexing.
index : The zero-based index in array at which copying begins.
Exceptions:
- ArgumentNullException : If the array is null.
- ArgumentOutOfRangeException : If the index is less than zero.
- InvalidCastException : If the type of the source HybridDictionary cannot be cast automatically to the type of the destination array.
- ArgumentException : If array is multidimensional OR the number of elements in the source HybridDictionary is greater than the available space from arrayIndex to the end of the destination array.
Below programs illustrate the use of HybridDictionary.CopyTo(Array, Int32) method:
Example 1:
// C# code to copy the HybridDictionary // entries to a one-dimensional Array // instance at the specified index. using System; using System.Collections; using System.Collections.Specialized; class GFG { // Driver code public static void Main() { // Creating a HybridDictionary named myDict HybridDictionary myDict = new HybridDictionary(); // Adding key/value pairs in myDict myDict.Add( "A" , "Apple" ); myDict.Add( "B" , "Banana" ); myDict.Add( "C" , "Cat" ); myDict.Add( "D" , "Dog" ); myDict.Add( "E" , "Elephant" ); myDict.Add( "F" , "Fish" ); // Creating a one-dimensional Array named myArr DictionaryEntry[] myArr = new DictionaryEntry[myDict.Count]; // copying the HybridDictionary entries // to a one-dimensional Array instance // at the specified index myDict.CopyTo(myArr, 0); for ( int i = 0; i < myArr.Length; i++) Console.WriteLine(myArr[i].Key + " --> " + myArr[i].Value); } } |
Output:
A --> Apple B --> Banana C --> Cat D --> Dog E --> Elephant F --> Fish
Example 2:
// C# code to copy the HybridDictionary // entries to a one-dimensional Array // instance at the specified index. using System; using System.Collections; using System.Collections.Specialized; class GFG { // Driver code public static void Main() { // Creating a HybridDictionary named myDict HybridDictionary myDict = new HybridDictionary(); // Adding key/value pairs in myDict myDict.Add( "I" , "first" ); myDict.Add( "II" , "second" ); myDict.Add( "III" , "third" ); myDict.Add( "IV" , "fourth" ); myDict.Add( "V" , "fifth" ); // Creating a one-dimensional Array named myArr DictionaryEntry[] myArr = new DictionaryEntry[myDict.Count]; // copying the HybridDictionary entries // to a one-dimensional Array instance // at the specified index // This should raise "ArgumentOutOfRangeException" // as index is less than 0 myDict.CopyTo(myArr, -2); for ( int i = 0; i < myArr.Length; i++) Console.WriteLine(myArr[i].Key + " --> " + myArr[i].Value); } } |
Runtime Error:
Unhandled Exception:
System.ArgumentOutOfRangeException: Index is less than zero.
Parameter name: index
Note:
- The elements are copied to the Array in the same order in which the enumerator iterates through the HybridDictionary.
- To copy only the keys in the HybridDictionary, use HybridDictionary.Keys.CopyTo.
- To copy only the values in the HybridDictionary, use HybridDictionary.Values.CopyTo.
- This method is an O(n) operation, where n is Count.
Reference:
Recommended Posts:
- C# | Copying the Hashtable elements to an Array Instance
- C# | Removing all entries from HybridDictionary
- C# | Copying the Collection<T> elements to an array
- C# | Copying the elements of ArrayList to a new array
- Copying the Queue elements to 1-D Array in C#
- C# | Copying BitArray elements to an Array
- C# | Copying the SortedList elements to an Array Object
- 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# | Copy OrderedDictionary elements to Array instance at the specified index
- C# | Adding the specified key and value into HybridDictionary
- C# | Gets or sets the value in HybridDictionary with specified key
- C# | HybridDictionary Class
- C# | Check the HybridDictionary for a specific key
- How to get Synchronize access to the HybridDictionary in C#
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.