C# | Copy OrderedDictionary elements to Array instance at the specified index (original) (raw)
Last Updated : 01 Feb, 2019
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 DictionaryEntry objects copied from OrderedDictionary. The Array must have zero-based indexing.index: it is the zero-based index in array at which copying begins.
Note: OrderedDictionary.CopyTo(Array, Int32) method doesn't provide any guarantee to maintain the order of the elements in the OrderedDictionary collection. Below programs illustrate the use of the above-discussed method:Example 1:
CSharp `
// C# code to copy OrderedDictionary to // 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 orderedDictionary named myDict
OrderedDictionary myDict = new OrderedDictionary();
// Adding key and value in myDict
myDict.Add("key1", "value1");
myDict.Add("key2", "value2");
myDict.Add("key3", "value3");
myDict.Add("key4", "value4");
myDict.Add("key5", "value5");
DictionaryEntry[] myArr = new DictionaryEntry[myDict.Count];
// Copying OrderedDictionary to Array
// instance at the specified index
myDict.CopyTo(myArr, 0);
// Displaying elements in myArr
for (int i = 0; i < myArr.Length; i++) {
Console.WriteLine(myArr[i].Key + "-->"
+ myArr[i].Value);
}
}
}
`
Output:
key3-->value3 key4-->value4 key5-->value5 key2-->value2 key1-->value1
Example 2:
CSharp `
// C# code to copy OrderedDictionary to // Array instance at the specified index // that give ArgumentOutOfRangeException // due to the negative index using System; using System.Collections; using System.Collections.Specialized;
class GFG {
// Driver code
public static void Main()
{
// Creating a orderedDictionary named myDict
OrderedDictionary myDict = new OrderedDictionary();
// Adding key and value in myDict
myDict.Add("C", "1");
myDict.Add("C++", "2");
myDict.Add("Java", "3");
myDict.Add("C#", "4");
DictionaryEntry[] myArr = new DictionaryEntry[myDict.Count];
// Copying OrderedDictionary to Array
// instance at the specified index
// This will raise "ArgumentOutOfRangeException"
// as index is less than 0
myDict.CopyTo(myArr, -2);
// Displaying elements in myArr
for (int i = 0; i < myArr.Length; i++) {
Console.WriteLine(myArr[i].Key + "-->"
+ myArr[i].Value);
}
}
}
`
Runtime Error:
Unhandled Exception: System.ArgumentOutOfRangeException: Non-negative number required. Parameter name: arrayIndex
Reference:
Similar Reads
- 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# | Insert an element into the ArrayList at the specified index ArrayList represents an ordered collection of an object that can be indexed individually. It is basically an alternative to an array. It also allows dynamic memory allocation, adding, searching and sorting items in the list. ArrayList.Insert(Int32, Object) method inserts an element into the ArrayLis 3 min read
- C# | Insert a new entry in OrderedDictionary with specified key and value OrderedDictionary.Insert(Int32, Object, Object) Method is used to inserts a new entry into the OrderedDictionary collection with the specified key and value at the specified index. Syntax: public void Insert (int index, object key, object value); Parameters: index: It is the zero-based index of type 3 min read
- C# | Insert into OrderedDictionary with key and value at specified index OrderedDictionary.Insert(Int32, Object, Object) method is used to insert a new entry into the OrderedDictionary collection with the specified key and value at the specified index. Syntax: public void Insert (int index, object key, object value); Parameters: index : It is the zero-based index at whic 3 min read
- C# | Remove the entry at specified index from OrderedDictionary OrderedDictionary.RemoveAt(Int32) method is used to remove the entry at the specified index from the OrderedDictionary collection. Syntax: public void RemoveAt (int index); Here, index is the zero-based index of the entry to remove. Exceptions: NotSupportedException : If the OrderedDictionary collec 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# | How to insert the elements of a collection into the List at the specified index List.InsertRange(Int32, IEnumerable) Method is used to insert the elements of a collection into the List at the specified index. Properties of List: It is different from the arrays. A list can be resized dynamically but arrays cannot. List class can accept null as a valid 3 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 a read-only copy of the OrderedDictionary OrderedDictionary.AsReadOnly method returns a read-only copy of the current OrderedDictionary collection. Syntax: public System.Collections.Specialized.OrderedDictionary AsReadOnly (); Return Value: A read-only copy of the current OrderedDictionary collection. Below given are some examples to unders 2 min read
- 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