Copying the HybridDictionary entries to an Array Instance in C#

The HybridDictionary class in C# provides a CopyTo() method that allows you to copy all dictionary entries to an array of DictionaryEntry objects. This method is useful when you need to convert dictionary data into array format for processing or storage.

The HybridDictionary automatically switches between a ListDictionary (for small collections) and a Hashtable (for larger collections) to optimize performance based on the number of elements.

Syntax

Following is the syntax for copying HybridDictionary entries to an array −

hybridDictionary.CopyTo(array, arrayIndex);

Parameters

  • array ? The one-dimensional array of type DictionaryEntry that is the destination.

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

Using CopyTo() with Starting Index

The following example demonstrates copying HybridDictionary entries to an array starting at index 2 −

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

public class Demo {
   public static void Main(){
      HybridDictionary dict = new HybridDictionary();
      dict.Add("1", "SUV");
      dict.Add("2", "AUV");
      dict.Add("3", "Utility Vehicle");
      dict.Add("4", "MUV");
      dict.Add("5", "Compact Car");
      dict.Add("6", "Convertible");
      
      Console.WriteLine("HybridDictionary Key and Value pairs...");
      foreach(DictionaryEntry entry in dict){
         Console.WriteLine("{0} and {1} ", entry.Key, entry.Value);
      }
      
      DictionaryEntry[] dictArr = new DictionaryEntry[10];
      Console.WriteLine("\nCopied to Array Instance...");
      dict.CopyTo(dictArr, 2);
      
      for (int i = 0; i 

The output of the above code is −

HybridDictionary Key and Value pairs...
1 and SUV 
2 and AUV 
3 and Utility Vehicle 
4 and MUV 
5 and Compact Car 
6 and Convertible 

Copied to Array Instance...
Key = , Value = 
Key = , Value = 
Key = 1, Value = SUV
Key = 2, Value = AUV
Key = 3, Value = Utility Vehicle
Key = 4, Value = MUV
Key = 5, Value = Compact Car
Key = 6, Value = Convertible
Key = , Value = 
Key = , Value = 

Using CopyTo() from Index Zero

This example shows copying dictionary entries starting from the beginning of the array −

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

public class Demo {
   public static void Main(){
      HybridDictionary dict = new HybridDictionary(10);
      dict.Add("1", "SUV");
      dict.Add("2", "AUV");
      dict.Add("3", "Utility Vehicle");
      dict.Add("4", "MUV");
      
      Console.WriteLine("HybridDictionary Key and Value pairs...");
      foreach(DictionaryEntry entry in dict){
         Console.WriteLine("{0} and {1} ", entry.Key, entry.Value);
      }
      
      DictionaryEntry[] dictArr = new DictionaryEntry[10];
      Console.WriteLine("\nCopied to Array Instance...");
      dict.CopyTo(dictArr, 0);
      
      for (int i = 0; i 

The output of the above code is −

HybridDictionary Key and Value pairs...
1 and SUV 
2 and AUV 
3 and Utility Vehicle 
4 and MUV 

Copied to Array Instance...
Key = 1, Value = SUV
Key = 2, Value = AUV
Key = 3, Value = Utility Vehicle
Key = 4, Value = MUV
Key = , Value = 
Key = , Value = 
Key = , Value = 
Key = , Value = 
Key = , Value = 
Key = , Value = 

Key Points

  • The target array must be of type DictionaryEntry[] to hold key-value pairs.

  • The array size should be sufficient to accommodate all dictionary entries plus the starting index.

  • Empty array positions will contain default DictionaryEntry values (empty key and value).

  • The CopyTo() method preserves the original order of dictionary entries.

Conclusion

The CopyTo() method of HybridDictionary provides an efficient way to transfer dictionary entries into an array format. This is particularly useful for scenarios where you need to process dictionary data using array-based operations or when integrating with APIs that expect array inputs.

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

168 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements