By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,510 Members | 1,810 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,510 IT Pros & Developers. It's quick & easy.

generics question

P: n/a
Hi...

I'm a little perplexed by some of the generics collection stuff.
Dictionary<Key, Valuebeing 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, MyClassinstead, 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

Jan 11 '08 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Hi,

see http://blogs.msdn.com/bclteam/archiv...15/396399.aspx

Kind regards,
Henning Krause

"Mark" <mm******@nospam.nospamwrote in message
news:A0**********************************@microsof t.com...
Hi...

I'm a little perplexed by some of the generics collection stuff.
Dictionary<Key, Valuebeing 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, MyClassinstead, 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
Jan 11 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.