Common Mistakes When Using Dictionary Like an Array in C#

Common Mistakes When Using Dictionary Like an Array in C#

Why treating Dictionary like an array leads to errors

In C#, a Dictionary uses square bracket syntax (like dict[key]), which might lead you to believe it works like an array. However, this is misleading. If you attempt to use a for loop with an index, you’ll run into runtime errors. Here's an example:
Dictionary<int, string> numbers = new Dictionary<int, string>()
{
    { 1, "one" },
    { 20, "twenty" },
    { 3, "three" },
    { 19, "nineteen" },
    { 5, "five" },
};

for (int i = 0; i < numbers.Count; i++)
{
    if (numbers[i] == "five")
    {
        Console.WriteLine(numbers[i]);
        break;
    }

    Console.WriteLine(numbers[i]);
}
This code throws the following error:

The given key '0' was not present in the dictionary.
  
Why? Because the dictionary has keys 1, 20, 3, 19, and 5 — not 0 through 4.

Dictionary is not an array

The syntax dict[key] looks like an array index, but it actually uses the C# feature called indexers. These allow object access via square brackets, but under the hood, it's a method call like this[key], not memory-based indexing like in an array. Therefore, trying to loop from 0 to Count is meaningless unless your keys happen to be 0-based integers, which is rarely the case.

The right way to iterate over a Dictionary

Instead of using a for loop, you should use foreach to iterate over the dictionary:
foreach (var pair in numbers)
{
    Console.WriteLine(pair.Key + " : " + pair.Value);
}
Or using the Keys collection:
foreach (var key in numbers.Keys)
{
    Console.WriteLine(key + " : " + numbers[key]);
}

Dictionaries do not preserve order

Another point to remember is that dictionaries do not preserve insertion order. Even if you add items in a specific sequence, the output order may be completely different. This is because dictionaries store items based on the hash code of the keys, not on their position.

How hash table and collisions work internally

When you insert a key into a dictionary, it is converted into a hash value. That hash determines where in the internal structure the item is stored. If multiple keys result in the same hash bucket (a hash collision), the dictionary forms a chain using a singly linked list internally. This happens under the hood and is handled automatically — you don't need to worry about it as a developer.

Summary

  • Dictionary[key] accesses a value by key, not by index like an array.
  • Using for (int i = 0; i < dict.Count; i++) is not safe unless your keys are sequential integers.
  • foreach is the correct way to iterate over dictionaries.
  • Dictionaries do not guarantee order — they are optimized for fast lookup, not sequential storage.
  • Internally, dictionaries use hash tables with automatic collision handling using linked structures.
If you’ve been thinking of Dictionary as a fancier array, now’s the time to stop — it’s a very different tool, designed for different needs.

Popular posts from this blog

Understanding Arrays as Reference Types in C#

Setting Up a Basic Follow Camera with Cinemachine 3.x

Understanding and Using ? (nullable) in C#