C# | Copy StringDictionary to Array at the specified index (original) (raw)
Last Updated : 01 Feb, 2019
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:
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# | Check if the StringDictionary contains a specific key StringDictionary.ContainsKey(String) method is used to check whether the StringDictionary contains a specific key or not. Syntax: public virtual bool ContainsKey (string key); Here, key is the key to locate in the StringDictionary. Return Value: This method returns true if the StringDictionary conta 2 min read
- C# | Check if the StringDictionary contains a specific value StringDictionary.ContainsValue(String) method is used to check whether the StringDictionary contains a specific value or not. Syntax: public virtual bool ContainsValue (string value); Here, value is the value to locate in the StringDictionary. The value can be null. Return Value: The method returns 2 min read
- C# | Insert at the 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. StringCollection.Insert(Int32, String) method is used to insert a string into the StringCollection 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
- 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