generics question

  • Thread starter Thread starter Mark
  • Start date Start date
M

Mark

Hi...

I'm a little perplexed by some of the generics collection stuff.
Dictionary<Key, Value> being the rough equivalent of Hashtable, I was
surprised to find there was no direct way to get to the Dictionary SyncRoot
to do a simple
lock(collection.SyncRoot) {}

I found a post somewhere saying that you had to cast the Dictionary to an
ICollection to get to the SyncRoot and do a lock.

In one case I replaced a member Hashtable with a Dictionary<String, MyClass>
and got the lock statements to work with the ((ICollection)Mytable).SyncRoot
cast, but now I've just run into this:

There's an old class we had that extended Hashtable. I changed it to extend
from Dictionary<String, MyClass> instead, but when I try to change
lock (this.SyncRoot)
to
lock ((ICollection)this).SyncRoot)
I get the compiler error
Using the generic type 'System.Collections.Generic.ICollection<T>' requires
'1' type arguments C:\test\MyClass.cs

a) I'm not clear why generics need the extra layer to get to the SyncRoot

b) I'm guessing it work on the member collection because I have an instance
of known types created, but I'm not clear why it won't work extending a
generic with the generic types defined.

I suppose I could just add an object member to my extension to sync on, but
I'm just trying to understand why it's so awkward to get to the one that's
buried in there.

Thanks
Mark
 
Back
Top