How to Convert Hashtable into an Array?

The Hashtable collection in C# is a non-generic collection of key-value pairs where each key is unique and non-null, while values can be duplicated or null. Converting a Hashtable into an array is a common operation when you need to work with the data in array format.

The Hashtable class provides the CopyTo method to efficiently transfer its elements to an array. This method copies Hashtable items as DictionaryEntry objects to a one-dimensional array.

Syntax

Following is the syntax for the CopyTo method

public virtual void CopyTo(Array array, int arrayIndex);

Parameters

  • array The destination one-dimensional Array that receives the DictionaryEntry objects. Must have zero-based indexing.

  • arrayIndex The zero-based index in the destination array where copying begins.

Using CopyTo with DictionaryEntry Array

The most common approach is to create a DictionaryEntry array to store complete key-value pairs

using System;
using System.Collections;

class Program {
   public static void Main() {
      // Create and populate hashtable
      Hashtable phonetics = new Hashtable();
      phonetics.Add("A", "Apple");
      phonetics.Add("B", "Banana");
      phonetics.Add("C", "Cat");
      phonetics.Add("D", "Dog");

      Console.WriteLine("Original Hashtable:");
      foreach(DictionaryEntry entry in phonetics) {
         Console.WriteLine($"{entry.Key} --> {entry.Value}");
      }

      // Create array to hold hashtable entries
      DictionaryEntry[] myArray = new DictionaryEntry[phonetics.Count];

      // Copy hashtable to array
      phonetics.CopyTo(myArray, 0);

      Console.WriteLine("\nArray after copying:");
      for (int i = 0; i < myArray.Length; i++) {
         Console.WriteLine($"{myArray[i].Key} --> {myArray[i].Value}");
      }
   }
}

The output of the above code is

Original Hashtable:
D --> Dog
B --> Banana
C --> Cat
A --> Apple

Array after copying:
D --> Dog
B --> Banana
C --> Cat
A --> Apple

Using CopyTo with Keys and Values

You can also copy only the keys or values using the Keys and Values properties

using System;
using System.Collections;

class Program {
   public static void Main() {
      // Create hashtable
      Hashtable colors = new Hashtable();
      colors.Add("R", "Red");
      colors.Add("G", "Green");
      colors.Add("B", "Blue");

      // Create arrays for keys and values
      string[] keyArray = new string[colors.Count];
      string[] valueArray = new string[colors.Count];

      // Copy keys and values separately
      colors.Keys.CopyTo(keyArray, 0);
      colors.Values.CopyTo(valueArray, 0);

      Console.WriteLine("Keys:");
      foreach(string key in keyArray) {
         Console.WriteLine(key);
      }

      Console.WriteLine("\nValues:");
      foreach(string value in valueArray) {
         Console.WriteLine(value);
      }
   }
}

The output of the above code is

Keys:
B
G
R

Values:
Blue
Green
Red

CopyTo with Existing Array

The CopyTo method can insert Hashtable elements at any position in an existing array

using System;
using System.Collections;

class Program {
   public static void Main() {
      // Create hashtable
      Hashtable numbers = new Hashtable();
      numbers.Add(1, "One");
      numbers.Add(2, "Two");

      // Create pre-populated array
      string[] myArray = new string[8];
      myArray[0] = "Start";
      myArray[1] = "Middle";
      myArray[7] = "End";

      Console.WriteLine("Array before copying:");
      PrintArray(myArray);

      // Copy hashtable values starting at index 3
      numbers.Values.CopyTo(myArray, 3);

      Console.WriteLine("\nArray after copying hashtable values at index 3:");
      PrintArray(myArray);
   }

   static void PrintArray(string[] arr) {
      for(int i = 0; i < arr.Length; i++) {
         Console.Write($"[{i}]: {arr[i] ?? "null"} ");
      }
      Console.WriteLine();
   }
}

The output of the above code is

Array before copying:
[0]: Start [1]: Middle [2]: null [3]: null [4]: null [5]: null [6]: null [7]: End 

Array after copying hashtable values at index 3:
[0]: Start [1]: Middle [2]: null [3]: Two [4]: One [5]: null [6]: null [7]: End 

Common Exceptions

Exception Condition
ArgumentNullException The destination array is null
ArgumentOutOfRangeException arrayIndex is less than zero
ArgumentException Array is multidimensional or insufficient space
InvalidCastException Hashtable type cannot be cast to destination array type

Conclusion

The CopyTo method provides an efficient way to convert Hashtable collections into arrays in C#. You can copy complete key-value pairs as DictionaryEntry objects, or copy just keys or values separately. The method allows flexible positioning within the destination array, making it versatile for various conversion scenarios.

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

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements