C# | Copying the HybridDictionary entries to an Array Instance (original) (raw)
Last Updated : 01 Feb, 2019
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:
CSHARP `
// 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:
CSHARP `
// 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:
Similar Reads
- C# | Copying the Hashtable elements to an Array Instance Hashtable.CopyTo(Array, Int32) Method is used to copy the elements of a Hashtable to a one-dimensional Array instance at the specified index.Syntax: public virtual void CopyTo (Array array, int arrayIndex); Parameters: array : The one-dimensional Array that is the destination of the DictionaryEntry 3 min read
- C# | Copying the SortedList elements to an Array Object SortedList.CopyTo(Array, Int32) Method is used to copy SortedList elements to a one-dimensional Array object, starting at the specified index in the array. Syntax: public virtual void CopyTo (Array array, int arrayIndex); Parameters: array: It is the one-dimensional Array object that is the destinat 2 min read
- C# | Copy ListDictionary to Array instance at the specified index ListDictionary.CopyTo(Array, Int32) method is used to copy the ListDictionary entries to a one-dimensional Array instance at the specified index. Syntax: public void CopyTo (Array array, int index); Parameters: array : It is the one-dimensional Array which is the destination of the DictionaryEntry o 3 min read
- C# | Copy StringDictionary to Array at the specified index StringDictionary.CopyTo(Array, Int32) method is used to copy the string dictionary values to a one-dimensional Array instance at the specified index. Syntax: public virtual void CopyTo (Array array, int index); Parameters: array: It is the one-dimensional Array which is the destination of the values 3 min read
- C# | Get an ICollection containing the keys in HybridDictionary HybridDictionary.Keys property is used to get an ICollection containing the keys in the HybridDictionary. Syntax: public System.Collections.ICollection Keys { get; } Return Value: It returns an ICollection containing the keys in the HybridDictionary. Below programs illustrate the use of HybridDictio 2 min read
- C# | Copy OrderedDictionary elements to Array instance at the specified index OrderedDictionary.CopyTo(Array, Int32) method is used to copy the OrderedDictionary elements to a one-dimensional Array object at the specified index. Syntax: public void CopyTo (Array array, int index); Parameters: array: It is the one-dimensional Array which is the destination of the DictionaryEnt 2 min read
- How to get Synchronize access to the HybridDictionary in C# HybridDictionary.SyncRoot Property is used to get an object which can be used to synchronize access to the HybridDictionary. It implements a linked list and hash table data structure. It implements IDictionary by using a ListDictionary when the collection is small, and a Hashtable when the collectio 3 min read
- C# | Get an ICollection containing the values in HybridDictionary HybridDictionary.Values property is used to get an ICollection containing the values in the HybridDictionary. Syntax: public System.Collections.ICollection Values { get; } Return Value: It returns an ICollection containing the values in the HybridDictionary. Below programs illustrate the use of Hybr 2 min read
- C# | Get an enumerator that iterates through the HybridDictionary HybridDictionary.GetEnumerator method is used to return an IDictionaryEnumerator that iterates through the HybridDictionary. Syntax: public System.Collections.IDictionaryEnumerator GetEnumerator (); Return Value: It returns an IDictionaryEnumerator (An Interface that enumerates the elements of a non 3 min read
- C# | Creating an empty case-sensitive HybridDictionary Class HybridDictionary() creates an empty case-sensitive HybridDictionary. Syntax: public HybridDictionary (); Below given are some examples to understand the implementation in a better way: Example 1: CSHARP // C# code to create an empty // case-sensitive HybridDictionary. using System; using System.Coll 2 min read