c# - Thread safe SortedDictionary -


i made class uses sorteddictionary store , manipulate data. class works great except when implemented in multi-threaded environment. now, make class thread safe writing wrapper class internal sorteddictionary class. use reader-writer locks implement now, i'm having problems writing wrapper class itself. specifically, i'm not sure how implement enumerator dictionary. here complete code class stands 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     {                  {              return _dict.comparer;         }     }      public int count     {                 {             return _dict.count;         }     }      public tvalue this[tkey key]     {                  {             return _dict[key];         }          set         {             _dict[key] = value;         }     }      public sorteddictionary<tkey, tvalue>.keycollection keys     {                 {             return new sorteddictionary<tkey,tvalue>.keycollection(_dict);         }     }      public sorteddictionary<tkey, tvalue>.valuecollection values     {                 {             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 compile code, error message:

'concurrentsorteddictionary' not implement interface member 'system.collections.ienumerable.getenumerator()'. 'concurrentsorteddictionary.getenumerator()' cannot implement 'system.collections.ienumerable.getenumerator()' because not have matching return type of 'system.collections.ienumerator'.

i looked @ several posts here relating reference:

how implement ienumerable in dictionary wrapper class implements ienumerable<foo>? what's best way of implementing thread-safe dictionary?

but don't see i'm doing wrong. assistance appreciated, thanks!

jason o

the problem here:

ienumerator<keyvaluepair<tkey, tvalue>> ienumerable<keyvaluepair<tkey, tvalue>>.getenumerator() {     return getenumerator(); }  public ienumerator<keyvaluepair<tkey, tvalue>> getenumerator() {     return _dict.getenumerator(); } 

you need:

public ienumerator<keyvaluepair<tkey, tvalue>> getenumerator() {     return _dict.getenumerator(); }  ienumerator ienumerable.getenumerator() {     return _dict.getenumerator(); } 

the second non-generic getenumerator() explicit interface implementation , needed unfortunate throwback days before generics , generic collections existed in c#.

see also: ienumerable<t> provides 2 getenumerator methods - difference between them? (and in particular michael b's answer).

however if want enumeration thread-safe along rest of class, may need write own thread-safe ienumerator type cooperates reader/writer locks in class!


Comments

Popular posts from this blog

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -

ruby on rails - In a controller spec, how to find a specific tag in the generated view? -