Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It's not difficult to roll your own <code>ReadOnlyDictionary&lt;K,V&gt;</code> wrapper class. Something like this:</p> <pre><code>public sealed class ReadOnlyDictionary&lt;TKey, TValue&gt; : IDictionary&lt;TKey, TValue&gt; { private readonly IDictionary&lt;TKey, TValue&gt; _dictionary; public ReadOnlyDictionary(IDictionary&lt;TKey, TValue&gt; dictionary) { if (dictionary == null) throw new ArgumentNullException("dictionary"); _dictionary = dictionary; } public bool ContainsKey(TKey key) { return _dictionary.ContainsKey(key); } public int Count { get { return _dictionary.Count; } } public IEnumerator&lt;KeyValuePair&lt;TKey, TValue&gt;&gt; GetEnumerator() { return _dictionary.GetEnumerator(); } public ICollection&lt;TKey&gt; Keys { get { return _dictionary.Keys; } } public bool TryGetValue(TKey key, out TValue value) { return _dictionary.TryGetValue(key, out value); } public ICollection&lt;TValue&gt; Values { get { return _dictionary.Values; } } public TValue this[TKey key] // Item { get { return _dictionary[key]; } } #region IDictionary&lt;TKey, TValue&gt; Explicit Interface Implementation void IDictionary&lt;TKey, TValue&gt;.Add(TKey key, TValue value) { throw new NotSupportedException("Dictionary is read-only."); } bool IDictionary&lt;TKey, TValue&gt;.Remove(TKey key) { throw new NotSupportedException("Dictionary is read-only."); } TValue IDictionary&lt;TKey, TValue&gt;.this[TKey key] // Item { get { return _dictionary[key]; } set { throw new NotSupportedException("Dictionary is read-only."); } } #endregion #region ICollection&lt;T&gt; Explicit Interface Implementation void ICollection&lt;KeyValuePair&lt;TKey, TValue&gt;&gt;.Add(KeyValuePair&lt;TKey, TValue&gt; item) { throw new NotSupportedException("Collection is read-only."); } void ICollection&lt;KeyValuePair&lt;TKey, TValue&gt;&gt;.Clear() { throw new NotSupportedException("Collection is read-only."); } bool ICollection&lt;KeyValuePair&lt;TKey, TValue&gt;&gt;.Contains(KeyValuePair&lt;TKey, TValue&gt; item) { return _dictionary.Contains(item); } void ICollection&lt;KeyValuePair&lt;TKey, TValue&gt;&gt;.CopyTo(KeyValuePair&lt;TKey, TValue&gt;[] array, int arrayIndex) { _dictionary.CopyTo(array, arrayIndex); } bool ICollection&lt;KeyValuePair&lt;TKey, TValue&gt;&gt;.IsReadOnly { get { return true; } } bool ICollection&lt;KeyValuePair&lt;TKey, TValue&gt;&gt;.Remove(KeyValuePair&lt;TKey, TValue&gt; item) { throw new NotSupportedException("Collection is read-only."); } #endregion #region IEnumerable Explicit Interface Implementation IEnumerator IEnumerable.GetEnumerator() { return ((IEnumerable)_dictionary).GetEnumerator(); } #endregion } </code></pre> <p>If you're using C#3 or later then you could knock-up a matching <code>AsReadOnly</code> extension method too:</p> <pre><code>public static class ReadOnlyDictionaryHelper { public static ReadOnlyDictionary&lt;TKey, TValue&gt; AsReadOnly&lt;TKey, TValue&gt;(this IDictionary&lt;TKey, TValue&gt; dictionary) { var temp = dictionary as ReadOnlyDictionary&lt;TKey, TValue&gt;; return temp ?? new ReadOnlyDictionary&lt;TKey, TValue&gt;(dictionary); } } </code></pre> <p>And then return the read-only wrapper from your property:</p> <pre><code>// in C#2 return new ReadOnlyDictionary&lt;string, OfTable&gt;(_folderData); // in C#3 or later return _folderData.AsReadOnly(); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload