Copy OrderedDictionary elements to Array instance at the specified index in C#

The OrderedDictionary class in C# provides the CopyTo() method to copy its elements to an array instance at a specified index. This method copies all key-value pairs as DictionaryEntry objects to the target array.

Syntax

Following is the syntax for copying OrderedDictionary elements to an array −

orderedDictionary.CopyTo(array, index);

Parameters

  • array − The one-dimensional array that is the destination of the elements copied from OrderedDictionary. The array must have zero-based indexing.

  • index − The zero-based index in the array at which copying begins.

Using CopyTo() with Exact Array Size

When copying to an array with the exact size of the OrderedDictionary, all elements are copied sequentially −

using System;
using System.Collections;
using System.Collections.Specialized;

public class Demo {
    public static void Main(){
        OrderedDictionary dict = new OrderedDictionary();
        dict.Add(1, "Harry");
        dict.Add(2, "Mark");
        dict.Add(3, "John");
        dict.Add(4, "Jacob");
        dict.Add(5, "Tim");
        
        Console.WriteLine("OrderedDictionary elements...");
        foreach(DictionaryEntry d in dict){
            Console.WriteLine(d.Key + " " + d.Value);
        }
        
        DictionaryEntry[] dictArr = new DictionaryEntry[dict.Count];
        Console.WriteLine("\nCopying to Array instance...");
        dict.CopyTo(dictArr, 0);
        
        for (int i = 0; i < dictArr.Length; i++) {
            Console.WriteLine("Key = " + dictArr[i].Key + ", Value = " + dictArr[i].Value);
        }
    }
}

The output of the above code is −

OrderedDictionary elements...
1 Harry
2 Mark
3 John
4 Jacob
5 Tim

Copying to Array instance...
Key = 1, Value = Harry
Key = 2, Value = Mark
Key = 3, Value = John
Key = 4, Value = Jacob
Key = 5, Value = Tim

Using CopyTo() with Larger Array Size

When the target array is larger than the OrderedDictionary, remaining array elements remain with their default values −

using System;
using System.Collections;
using System.Collections.Specialized;

public class Demo {
    public static void Main(){
        OrderedDictionary dict = new OrderedDictionary();
        dict.Add(1, 10);
        dict.Add(2, 20);
        
        Console.WriteLine("OrderedDictionary elements...");
        foreach(DictionaryEntry d in dict){
            Console.WriteLine(d.Key + " " + d.Value);
        }
        
        DictionaryEntry[] dictArr = new DictionaryEntry[5];
        Console.WriteLine("\nCopying to Array instance...");
        dict.CopyTo(dictArr, 0);
        
        for (int i = 0; i < dictArr.Length; i++) {
            Console.WriteLine("Key = " + dictArr[i].Key + ", Value = " + dictArr[i].Value);
        }
    }
}

The output of the above code is −

OrderedDictionary elements...
1 10
2 20

Copying to Array instance...
Key = 1, Value = 10
Key = 2, Value = 20
Key = , Value = 
Key = , Value = 
Key = , Value = 

Using CopyTo() with Non-Zero Starting Index

You can specify a different starting index to copy elements at a specific position in the target array −

using System;
using System.Collections;
using System.Collections.Specialized;

public class Demo {
    public static void Main(){
        OrderedDictionary dict = new OrderedDictionary();
        dict.Add("A", "Apple");
        dict.Add("B", "Banana");
        dict.Add("C", "Cherry");
        
        Console.WriteLine("OrderedDictionary elements...");
        foreach(DictionaryEntry d in dict){
            Console.WriteLine(d.Key + " " + d.Value);
        }
        
        DictionaryEntry[] dictArr = new DictionaryEntry[6];
        Console.WriteLine("\nCopying to Array starting at index 2...");
        dict.CopyTo(dictArr, 2);
        
        for (int i = 0; i < dictArr.Length; i++) {
            Console.WriteLine("Index " + i + ": Key = " + dictArr[i].Key + ", Value = " + dictArr[i].Value);
        }
    }
}

The output of the above code is −

OrderedDictionary elements...
A Apple
B Banana
C Cherry

Copying to Array starting at index 2...
Index 0: Key = , Value = 
Index 1: Key = , Value = 
Index 2: Key = A, Value = Apple
Index 3: Key = B, Value = Banana
Index 4: Key = C, Value = Cherry
Index 5: Key = , Value = 

Conclusion

The CopyTo() method of OrderedDictionary efficiently copies all key-value pairs to a DictionaryEntry array at the specified index. This method preserves the insertion order of elements and is useful for converting OrderedDictionary data into array format for further processing.

Updated on: 2026-03-17T07:04:36+05:30

198 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements