Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / February 2008

Tip: Looking for answers? Try searching our database.

C# SynchronizedKeyedCollection

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
NvrBst - 22 Feb 2008 02:24 GMT
I've been playing around with a bunch of synchronization classes latly
but I can't seem to get the following one  to work?  When I do
"SynchronizedKeyedCollection<int,int> myDic = new
SynchronizedKeyedCollection<int,int>();" I get the error:

Cannot create an instance of the abstract class or interface
'System.Collections.Generic.SynchronizedKeyedCollection<int,int>'

There is also no intellisence for completing the "new ..." keyword
like other classes.  Could someone give me a quick example on how to
use this class? (I see on MSDN it has a constructor, shouldn't I be
able to use it like I would a Dictionary<T,K>?)

NB

Note: I have .NET 3.5, and using VS2008 (Team System).
System.ServiceModel is referenced and IntelliSence is highlighting the
class like it can see it.  I'm also able to use the
"SynchronizedCollection<int>" like I do a List<T>
Marc Gravell - 22 Feb 2008 04:57 GMT
It is abstract, so if it has a constructor then it is proteted...

Basically, unless you provide your own concrete implementation of
this, you can't use it. The only concrete subclass (that I can see) is
System.ServiceModel.UriSchemeKeyedCollection.

Of course, I *still* don't believe they gain much ;-p

Marc
NvrBst - 22 Feb 2008 19:15 GMT
> It is abstract, so if it has a constructor then it is proteted...
>
[quoted text clipped - 5 lines]
>
> Marc

Ahh, your right.  When I read "SychronizedCollection<T>" I thought it
would autolock at the lower levels to ensure no crashing and the such
(Good for quick, simple use of a thread safe collection).  But now
when I read it again on MSDN "The SynchronizedCollection<(Of <(T>)>)
stores data in a List<(Of <(T>)>) container and provides an object
that can be set and used to synchronize access to the collection so
that it is thread-safe.".  This means I have to lock manually (just
like I would with List<T>) before using it?  Basically Identical to a
List<T> excpet it has a SyncLock object built inside?

That is almost useless, if I'm understanding it right :)  If I'm
mistaken (and the collection does auto lock at lower levels), please
correct me :)

NB
Marc Gravell - 22 Feb 2008 19:45 GMT
> Ahh, your right.  When I read "SychronizedCollection<T>" I thought it
> would autolock at the lower levels to ensure no crashing and the such

It appears to lock for individual operations; but *generally* in my
experience you need to consider multiple operations as a transaction
for it to be useful (whether that is the duration of a foreach, or a
"contains/add" pair, etc).

But essentially all the methods on this do lock the sync-object -
something like:

public Foo Bar(...) {
 lock(SyncRoot) {
   return Items.Bar();
 }
}

(if you see what I mean)

Marc
NvrBst - 22 Feb 2008 19:50 GMT
> > Ahh, your right.  When I read "SychronizedCollection<T>" I thought it
> > would autolock at the lower levels to ensure no crashing and the such
[quoted text clipped - 17 lines]
>
> Marc

Ahh, it has its uses then!  :)  Thanks for the update, and all the
help ya've given.

NB

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.