ReadOnlyCollection and Multiple Threads

  • Thread starter Thread starter Chris Mullins [MVP]
  • Start date Start date
C

Chris Mullins [MVP]

I've been really leveraging the ReadOnlyCollection class the last few weeks,
figuring that it works exactly as it's name describes.

Yesterday I was rudely reminded that reading the documentation for a class
is very important, and that making silly assumptions is also bad.

It turns out the ReadOnlyCollection isn't readonly at all, and that
iterating over it isn't a good thing. I looked all over the Web and various
Newsgroups for other people who have run across this, and all of the
examples I found were wrong. Many, many people have made the same mistake
that I did.

http://www.coversant.com/Default.aspx?tabid=88&EntryID=34

In a nutshell: The ReadOnlyCollection class is not thread safe. If a
different thread changes the underlying IList<T> then the contents of the
ReadOnlyCollection change, and all of the standard threading problems arise.

The Documentation says exactly this, so it's simply a case of failing to
RTFM.
 
Chris Mullins said:
I've been really leveraging the ReadOnlyCollection class the last few
weeks, figuring that it works exactly as it's name describes.

Yesterday I was rudely reminded that reading the documentation for a class
is very important, and that making silly assumptions is also bad.

It turns out the ReadOnlyCollection isn't readonly at all, and that
iterating over it isn't a good thing. I looked all over the Web and
various Newsgroups for other people who have run across this, and all of
the examples I found were wrong. Many, many people have made the same
mistake that I did.

http://www.coversant.com/Default.aspx?tabid=88&EntryID=34

In a nutshell: The ReadOnlyCollection class is not thread safe. If a
different thread changes the underlying IList<T> then the contents of the
ReadOnlyCollection change, and all of the standard threading problems
arise.

The Documentation says exactly this, so it's simply a case of failing to
RTFM.

Bottom line - the class is misnamed. It should be named: View<T>. The
given name is completely misleading in alomst every connotation.

-cd
 
I agree with ya that View<T> would have been a much better name.

The "ReadOnly" name really is very misleading, and has proven to be so not
only to me, but other people all over the web. Typically their names in the
framework are pretty good, but this one certainly could use a revisit.
 
What if "HashTable" really was implemented as an Linked List? Or ArrayList
didn't use an Array? Or if Queue really acted as a Stack or a Deque? Or
SkipList was implemented as a HashSet?

Microsoft's track record of naming within the Framework is excellent - this
class just didn't do the trick for me, and (based on other things people
have said on the web), I wasn't the only one mislead by the name.
 
Back
Top