473,385 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

generics question

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
1 2703
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Fred | last post by:
Is there a way to use generics with vs.net 2003?? I was searching around and I saw gyro but does it works with vs.net 2003?? if it does how am I suppouse to install it. If it doesnt is there...
27
by: Bernardo Heynemann | last post by:
How can I use Generics? How can I use C# 2.0? I already have VS.NET 2003 Enterprise Edition and still can´t use generics... I´m trying to make a generic collection myCollection<vartype> and...
12
by: Michael S | last post by:
Why do people spend so much time writing complex generic types? for fun? to learn? for use? I think of generics like I do about operator overloading. Great to have as a language-feature, as...
13
by: Luc Vaillant | last post by:
I try to compare to values of generic value type T in a generic class as follow: public class C<T> where T : struct { private T value1; private T value2; C(T value1, T value2) {
1
by: Peter Kirk | last post by:
Hi I have never used generics before, and I was wondering if the following sort of use was acceptable/normal for a method: public IList<IPerson> GetPersons() { IList<IPerson> personList =...
18
by: riftimes | last post by:
Hello, would you help me to find totorials with examples about generics and Dictionary thank you.
11
by: hammad.awan_nospam | last post by:
Hello, I'm wondering if it's possible to do the following with Generics: Let's say I have a generic member variable as part of a generic class like this: List<DLinqQuery<TDataContext>>...
1
by: Kevin S. Goff | last post by:
Hi, all, Hopefully this will make sense: I have 2 classes that implement the same generic interface. public interface IAgingReport<T> { T GetAgingReport(DateTime dAsOfDate); }
7
by: SpotNet | last post by:
Hello NewsGroup, Reading up on Generics in the .NET Framework 2.0 using C# 2005 (SP1), I have a question on the application of Generics. Knowingly, Generic classes are contained in the...
13
by: rkausch | last post by:
Hello everyone, I'm writing because I'm frustrated with the implementation of C#'s generics, and need a workaround. I come from a Java background, and am currently writing a portion of an...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.