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 objects copied from ListDictionary. 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 ListDictionary cannot be cast automatically to the type of the destination array.
  • ArgumentException : If the array is multidimensional OR the number of elements in the source ListDictionary is greater than the available space from index to the end of the destination array.

Below given are some examples to understand the implementation in a better way:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# code to copy ListDictionary 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 ListDictionary named myDict
        ListDictionary myDict = new ListDictionary();
  
        myDict.Add("Australia", "Canberra");
        myDict.Add("Belgium", "Brussels");
        myDict.Add("Netherlands", "Amsterdam");
        myDict.Add("China", "Beijing");
        myDict.Add("Russia", "Moscow");
        myDict.Add("India", "New Delhi");
  
        DictionaryEntry[] myArr = new DictionaryEntry[myDict.Count];
  
        // Copying ListDictionary 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);
        }
    }
}

chevron_right


Output:

Australia-->Canberra
Belgium-->Brussels
Netherlands-->Amsterdam
China-->Beijing
Russia-->Moscow
India-->New Delhi

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# code to copy ListDictionary 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 ListDictionary named myDict
        ListDictionary myDict = new ListDictionary();
  
        myDict.Add("Australia", "Canberra");
        myDict.Add("Belgium", "Brussels");
        myDict.Add("Netherlands", "Amsterdam");
        myDict.Add("China", "Beijing");
        myDict.Add("Russia", "Moscow");
        myDict.Add("India", "New Delhi");
  
        DictionaryEntry[] myArr = new DictionaryEntry[myDict.Count];
  
        // Copying ListDictionary to Array
        // instance at the specified index
        // This should 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);
        }
    }
}

chevron_right


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 ListDictionary.
  • To copy only the keys in the ListDictionary, use ListDictionary.Keys.CopyTo.
  • To copy only the values in the ListDictionary, use ListDictionary.Values.CopyTo.
  • This method is an O(n) operation, where n is Count.

Reference:



My Personal Notes arrow_drop_up

In love with a semicolon because sometimes i miss it so badly)

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.