Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Is there anything built into the core C# libraries that can give me an immutable Dictionary?

Something along the lines of Java's:

Collections.unmodifiableMap(myMap); 

And just to clarify, I am not looking to stop the keys / values themselves from being changed, just the structure of the Dictionary. I want something that fails fast and loud if any of IDictionary's mutator methods are called (Add, Remove, Clear).

like image 313
serg10 Avatar asked Aug 29 '08 18:08

serg10


People also ask

What does |= mean in C?

The ' |= ' symbol is the bitwise OR assignment operator.

Do loops in C?

Syntax. do { statement(s); } while( condition ); Notice that the conditional expression appears at the end of the loop, so the statement(s) in the loop executes once before the condition is tested. If the condition is true, the flow of control jumps back up to do, and the statement(s) in the loop executes again.


1 Answers

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.

like image 152
dbkk Avatar answered Sep 21 '22 14:09

dbkk