|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
ReadOnlyCollection and Multiple Threadsfiguring 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 Show quote
"Chris Mullins [MVP]" <cmull***@yahoo.com> wrote in message Bottom line - the class is misnamed. It should be named: View<T>. The 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. 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. -- 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 > > 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 > > > 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 >> >> >> |
|||||||||||||||||||||||