Home All Groups Group Topic Archive Search About

ReadOnlyCollection and Multiple Threads

Author
22 Mar 2007 5:16 PM
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, MCSD.NET, MCPD:Enterprise, Microsoft C# MVP
http://www.coversant.com/blogs/cmullins

Author
22 Mar 2007 8:23 PM
Carl Daniel [VC++ MVP]
Show quote
"Chris Mullins [MVP]" <cmull***@yahoo.com> wrote in message
news:em%23dOXKbHHA.260@TK2MSFTNGP02.phx.gbl...
> 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
Author
22 Mar 2007 10:14 PM
Chris Mullins [MVP]
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.

--
Chris Mullins, MCSD.NET, MCPD:Enterprise, Microsoft C# MVP
http://www.coversant.com/blogs/cmullins

Show quote
"Carl Daniel [VC++ MVP]" <cpdaniel_remove_this_and_nospam@mvps.org.nospam>
wrote in message news:uBXgl$LbHHA.2188@TK2MSFTNGP04.phx.gbl...
> "Chris Mullins [MVP]" <cmull***@yahoo.com> wrote in message
> news:em%23dOXKbHHA.260@TK2MSFTNGP02.phx.gbl...
>> 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
>
>
Author
27 Mar 2007 11:16 PM
KierenH
ReadOnlyCollection is well documented with samples, can't ask for much more :)

Show quote
"Chris Mullins [MVP]" wrote:

> 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, MCSD.NET, MCPD:Enterprise, Microsoft C# MVP
> http://www.coversant.com/blogs/cmullins
>
>
>
Author
27 Mar 2007 11:29 PM
Chris Mullins [MVP]
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.

--
Chris Mullins, MCSD.NET, MCPD:Enterprise, Microsoft C# MVP
http://www.coversant.com/blogs/cmullins

Show quote
"KierenH" <Kier***@discussions.microsoft.com> wrote in message
news:89C047EF-BB23-4E41-8034-3AEDB22CFDCA@microsoft.com...
> ReadOnlyCollection is well documented with samples, can't ask for much
> more :)
>
> "Chris Mullins [MVP]" wrote:
>
>> 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, MCSD.NET, MCPD:Enterprise, Microsoft C# MVP
>> http://www.coversant.com/blogs/cmullins
>>
>>
>>

AddThis Social Bookmark Button