Does C# have a way of giving me an immutable Dictionary?

No, but a wrapper is rather trivial:

public class ReadOnlyDictionary<TKey, TValue> : IDictionary<TKey, TValue>
{
    IDictionary<TKey, TValue> _dict;

    public ReadOnlyDictionary(IDictionary<TKey, TValue> backingDict)
    {
        _dict = backingDict;
    }

    public void Add(TKey key, TValue value)
    {
        throw new InvalidOperationException();
    }

    public bool ContainsKey(TKey key)
    {
        return _dict.ContainsKey(key);
    }

    public ICollection<TKey> Keys
    {
        get { return _dict.Keys; }
    }

    public bool Remove(TKey key)
    {
        throw new InvalidOperationException();
    }

    public bool TryGetValue(TKey key, out TValue value)
    {
        return _dict.TryGetValue(key, out value);
    }

    public ICollection<TValue> Values
    {
        get { return _dict.Values; }
    }

    public TValue this[TKey key]
    {
        get { return _dict[key]; }
        set { throw new InvalidOperationException(); }
    }

    public void Add(KeyValuePair<TKey, TValue> item)
    {
        throw new InvalidOperationException();
    }

    public void Clear()
    {
        throw new InvalidOperationException();
    }

    public bool Contains(KeyValuePair<TKey, TValue> item)
    {
        return _dict.Contains(item);
    }

    public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
    {
        _dict.CopyTo(array, arrayIndex);
    }

    public int Count
    {
        get { return _dict.Count; }
    }

    public bool IsReadOnly
    {
        get { return true; }
    }

    public bool Remove(KeyValuePair<TKey, TValue> item)
    {
        throw new InvalidOperationException();
    }

    public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
    {
        return _dict.GetEnumerator();
    }

    System.Collections.IEnumerator 
           System.Collections.IEnumerable.GetEnumerator()
    {
        return ((System.Collections.IEnumerable)_dict).GetEnumerator();
    }
}

Obviously, you can change the this[] setter above if you want to allow modifying values.


As far as I know, there is not. But maybe you can copy some code (and learn a lot) from these articles:

  • Immutability in C# Part One: Kinds of Immutability
  • Immutability in C# Part Two: A Simple Immutable Stack
  • Immutability in C# Part Three: A Covariant Immutable Stack
  • Immutability in C# Part Four: An Immutable Queue
  • Immutability in C# Part Five: LOLZ
  • Immutability in C# Part Six: A Simple Binary Tree
  • Immutability in C# Part Seven: More on Binary Trees
  • Immutability in C# Part Eight: Even More On Binary Trees
  • Immutability in C# Part Nine: Academic? Plus my AVL tree implementation
  • Immutability in C# Part 10: A double-ended queue
  • Immutability in C# Part Eleven: A working double-ended queue

With the release of .NET 4.5, there is a new ReadOnlyDictionary class. You simply pass an IDictionary to the constructor to create the immutable dictionary.

Here is a helpful extension method which can be used to simplify creating the readonly dictionary.