C# | Copy StringDictionary to Array at the specified index Last Updated : 01 Feb, 2019 Comments Improve Suggest changes Like Article Like Report 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: CSHARP // 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: CSHARP // 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: https://docs.microsoft.com/en-us/dotnet/api/system.collections.specialized.stringdictionary.copyto?view=netframework-4.7.2 Comment More infoAdvertise with us Next Article C# | Copy StringDictionary to Array at the specified index S Sahil_Bansall Follow Improve Article Tags : C# CSharp-method CSharp-Collections-Namespace CSharp-Specialized-StringDictionary CSharp-Specialized-Namespace +1 More Similar Reads C# | Copy StringCollection at the specified index of array StringCollection class is a new addition to the .NET Framework class library that represents a collection of strings. StringCollection class is defined in the System.Collections.Specialized namespace. StringCollection.CopyTo(String[], Int32) method is used to copy the entire StringCollection values 3 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 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 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 obje 3 min read C# | Get or set the value associated with the specified key in StringDictionary StringDictionary is a specialized collection. It is found in the System.Collections.Specialized namespace. It only allows string keys and string values. It suffers from performance problems. It implements a hash table with the key and the value strongly typed to be strings rather than objects. Below 2 min read C# | Get a collection of values in the StringDictionary StringDictionary.Values property is used to get a collection of values in the StringDictionary. Syntax: public virtual System.Collections.ICollection Values { get; } Return Value: An ICollection that provides the values in the StringDictionary. Example 1: CSHARP // C# code to get a collection // of 2 min read C# | Get a collection of keys in the StringDictionary StringDictionary.Keys property is used to get a collection of keys in the StringDictionary. Syntax: public virtual System.Collections.ICollection Keys { get; } Return Value: An ICollection that provides the keys in the StringDictionary. Below given are some examples to understand the implementation 2 min read How to create a StringDictionary in C# StringDictionary() constructor is used to initialize a new instance of the StringDictionary class which will be empty and will have the default initial capacity. StringDictionary is a specialized collection. This class comes under the System.Collections.Specialized namespace. It only allows string k 2 min read C# | Get or Set at specified index in StringCollection StringCollection class is a new addition to the .NET Framework class library that represents a collection of strings. StringCollection class is defined in the System.Collections.Specialized namespace. Properties: StringCollection accepts null as a valid value and allows duplicate elements. String co 2 min read C# | Add key and value into StringDictionary StringDictionary.Add(String, String) method is used to add an entry with the specified key and value into the StringDictionary. Syntax: public virtual void Add (string key, string value); Parameters: key: It is the key of the entry which is to be added. value: It is the value of the entry which is t 2 min read Like