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 copied from the StringDictionary.
- index: It is the index in the array where copying begins.
Exceptions:
- ArgumentNullException : If the key is null.
- ArgumentOutOfRangeException : If the index is less than the lower bound of array.
- ArgumentException : If the array is multidimensional Or the number of elements in the StringDictionary is greater than the available space from index to the end of array.
Below programs illustrate the use of StringDictionary.CopyTo(Array, Int32) method:
Example 1:
// C# code to copy StringDictionary // to Array at the specified index using System; using System.Collections; using System.Collections.Specialized; class GFG { // Driver code public static void Main() { // Creating a StringDictionary named myDict StringDictionary myDict = new StringDictionary(); // Adding key and value into the StringDictionary myDict.Add("A", "Apple"); myDict.Add("B", "Banana"); myDict.Add("C", "Cat"); myDict.Add("D", "Dog"); myDict.Add("E", "Elephant"); // Creating an Array named myArr DictionaryEntry[] myArr = { new DictionaryEntry(), new DictionaryEntry(), new DictionaryEntry(), new DictionaryEntry(), new DictionaryEntry() }; // Copying StringDictionary to // Array at the specified index myDict.CopyTo(myArr, 0); // Displaying key and value pairs in Array myArr for (int i = 0; i < myArr.Length; i++) { Console.WriteLine(myArr[i].Key + " " + myArr[i].Value); } } } |
Output:
d Dog b Banana c Cat e Elephant a Apple
Example 2:
// C# code to copy StringDictionary // to Array at the specified index using System; using System.Collections; using System.Collections.Specialized; class GFG { // Driver code public static void Main() { // Creating a StringDictionary named myDict StringDictionary myDict = new StringDictionary(); // Adding key and value into the StringDictionary myDict.Add("A", "Apple"); myDict.Add("B", "Banana"); myDict.Add("C", "Cat"); myDict.Add("D", "Dog"); myDict.Add("E", "Elephant"); // Creating an Array named myArr DictionaryEntry[] myArr = { new DictionaryEntry(), new DictionaryEntry(), new DictionaryEntry() }; // Copying StringDictionary to // Array at the specified index // This should raise "ArgumentException" as // the number of elements in the StringDictionary // is greater than the available space from // index to the end of array. myDict.CopyTo(myArr, 0); // Displaying key and value pairs in Array myArr for (int i = 0; i < myArr.Length; i++) { Console.WriteLine(myArr[i].Key + " " + myArr[i].Value); } } } |
Runtime Error:
Unhandled Exception:
System.ArgumentException: Destination array is not long enough to copy all the items in the collection. Check array index and length.
Note:
- The elements copied to the Array are sorted in the same order that the enumerator iterates through the StringDictionary.
- This method is an O(n) operation, where n is Count.
Reference:
Recommended Posts:
- C# | Remove entry with specified key from the StringDictionary
- C# | Get or set the value associated with the specified key in StringDictionary
- C# | Gets or sets the value at the specified key in StringDictionary
- C# | Copy StringCollection at the specified index of array
- C# | Copy ListDictionary to Array instance at the specified index
- C# | Copy OrderedDictionary elements to Array instance at the specified index
- Creating an Index From the Specified Index at the Start of a Collection in C#
- Creating an Index From the End of a Collection at a Specified Index Position in C#
- C# | Get the number of key/value pairs in the StringDictionary
- C# | Check if the StringDictionary contains a specific value
- C# | Check if the StringDictionary contains a specific key
- C# | Add key and value into StringDictionary
- C# | Removing all entries from the StringDictionary
- C# | Check if StringDictionary is synchronized (thread safe)
- C# | Get an enumerator that iterates through the stringDictionary
- C# | Get a collection of keys in the StringDictionary
- C# | Get a collection of values in the StringDictionary
- C# | StringDictionary Class
- C# | Check if two StringDictionary objects are equal or not
- How to get Synchronize access to the StringDictionary 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.

