C# Dictionary Articles

The Dictionary type in the base class library is one of the most important ones you need to use for your C# programs. It is an implementation of a hashtable, which is an extremely efficient way to store keys for lookup. The Dictionary in .NET is well-designed, and this section shows lots of ways you can use it.

Introduction to Dictionary

First, this simple program demonstrates how the Dictionary can be used with type parameters to store keys and values of specific types. You can store and then retrieve keys and values from the Dictionary collection.

--- Program that uses Dictionary [C#] ---

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        // Use the dictionary.
        Dictionary<string, int> dict = new Dictionary<string, int>();
        dict.Add("cat", 1);
        dict.Add("dog", 4);

        Console.WriteLine(dict["cat"]);
        Console.WriteLine(dict["dog"]);
    }
}

--- Output of the program ---

1
4

More examples. These articles form the core of the Dictionary tutorials here. They show many examples of using Dictionary and are useful for many developers. The first article linked is recommended as an introduction.

See Dictionary Examples, Keys and Values.

See Dictionary Lookup Comparison.

Sorting Dictionary

How can you sort the keys or values from a Dictionary? You cannot do this directly; rather, you can acquire a List of the relevant items and then sort that. These articles show you how.

See Sort Dictionary Keys.

See Sort Dictionary Values.

Methods

There are many built-in methods on the Dictionary type. They allow you to look up keys (with ContainsKey); or search for values (with ContainsValue). Also, you can check out the ToDictionary method which creates a Dictionary from another type instance.

See ContainsKey Dictionary Method.

See ContainsValue Dictionary Example.

See Count Dictionary, Using Count Property.

See TryGetValue Method.

See ToDictionary Method.

Custom methods

Unfortunately, not all requirements you may have with your collections are satisfied by the built-in methods. For this reason, you will require some custom methods. These methods provide a sample of the custom routines you can construct.

See Dictionary Equals Method.

See Copy Dictionary.

See Combine Dictionary Keys.

Using IEqualityComparer

The Dictionary type uses an IEqualityComparer implementation to determine the hash code for its keys. You can implement this interface with a class yourself; this article shows you how you can do this.

See IEqualityComparer Dictionary.

Dictionary instances

Additionally, Dot Net Perls provides several examples of Dictionary instances used in different contexts. These articles are less useful but still can reveal useful tips regarding the syntax of the Dictionary type.

See Case-Insensitive Dictionary.

See Dictionary Object and Member.

See Dictionary Parameter and Return Value.

See Static Dictionary.

See Stopword Dictionary.

See Var Dictionary Example.

Key types. Also, if you just need a Dictionary with integer keys or string keys, these articles present very specific examples.

See Dictionary Int Examples.

See String Dictionary.

Dictionary performance

How fast is the Dictionary type? Fortunately, the Dictionary is well-optimized by the .NET Framework developers, but there are still some ways you can use it that influence performance. These articles reveal aspects of the Dictionary's performance.

See Array and Dictionary Test, Integer Lookups.

See Dictionary Optimization Tip.

See Dictionary Order.

See Dictionary Size and Performance.

See Dictionary String Key Length.

See Dictionary StringComparer Tip.

See Dictionary vs. List Lookup Time.

© 2007-2010 Sam Allen. All rights reserved.

Dot Net Perls  Sam Allen