I created a class that uses a SortedDictionary to store and process data. A class works fine, except when implemented in a multi-threaded environment. Now I would like to make the class thread safe by writing a wrapper class for the inner class SortedDictionary. I would like to use Lock-Writer Locks to implement this, but at the moment I have problems writing the wrapper class itself. In particular, I'm not sure how to implement Enumerator for a dictionary. Here is my complete code for the class that is standing now.
public class ConcurrentSortedDictionary<TKey, TValue> : IEnumerable<KeyValuePair<TKey, TValue>> { #region Variables SortedDictionary<TKey, TValue> _dict; #endregion #region Constructors public ConcurrentSortedDictionary() { _dict = new SortedDictionary<TKey, TValue>(); } public ConcurrentSortedDictionary(IComparer<TKey> comparer) { _dict = new SortedDictionary<TKey, TValue>(comparer); } public ConcurrentSortedDictionary(IDictionary<TKey, TValue> dictionary) { _dict = new SortedDictionary<TKey, TValue>(dictionary); } public ConcurrentSortedDictionary(IDictionary<TKey, TValue> dictionary, IComparer<TKey> comparer) { _dict = new SortedDictionary<TKey, TValue>(dictionary, comparer); } #endregion #region Properties public IComparer<TKey> Comparer { get { return _dict.Comparer; } } public int Count { get { return _dict.Count; } } public TValue this[TKey key] { get { return _dict[key]; } set { _dict[key] = value; } } public SortedDictionary<TKey, TValue>.KeyCollection Keys { get { return new SortedDictionary<TKey,TValue>.KeyCollection(_dict); } } public SortedDictionary<TKey, TValue>.ValueCollection Values { get { return new SortedDictionary<TKey, TValue>.ValueCollection(_dict); } } #endregion #region Methods public void Add(TKey key, TValue value) { _dict.Add(key, value); } public void Clear() { _dict.Clear(); } public bool ContainsKey(TKey key) { return _dict.ContainsKey(key); } public bool ContainsValue(TValue value) { return _dict.ContainsValue(value); } public void CopyTo(KeyValuePair<TKey, TValue>[] array, int index) { _dict.CopyTo(array, index); } public override bool Equals(Object obj) { return _dict.Equals(obj); } IEnumerator<KeyValuePair<TKey, TValue>> IEnumerable<KeyValuePair<TKey, TValue>>.GetEnumerator() { return GetEnumerator(); } public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator() { return _dict.GetEnumerator(); } public override int GetHashCode() { return _dict.GetHashCode(); } public bool Remove(TKey key) { return _dict.Remove(key); } public override string ToString() { return _dict.ToString(); } public bool TryGetValue(TKey key, out TValue value) { return _dict.TryGetValue(key, out value); } #endregion }
When I compile the code, I get an error message:
'ConcurrentSortedDictionary' does not implement the member interface of 'System.Collections.IEnumerable.GetEnumerator ()'. 'ConcurrentSortedDictionary.GetEnumerator ()' cannot implement 'System.Collections.IEnumerable.GetEnumerator ()' because it does not have the corresponding return type 'System.Collections.IEnumerator'.
I reviewed a few posts related to this as a reference:
How to implement IEnumerable in my Dictionary wrapper class that implements IEnumerable <Foo>? What is the best way to implement a thread safe dictionary?
but I do not see what I am doing wrong. Any help is much appreciated, thanks!
Jason Oh