This post originated from an RSS feed registered with .NET Buzz
by Jeff Key.
Original Post: To ReadOnly or not to ReadOnly?
Feed Title: Jeff Key
Feed URL: http://www.asp.net/err404.htm?aspxerrorpath=/jkey/Rss.aspx
Feed Description: Topics revolve around .NET and the Windows platform.
BradA occasionally posts API design questions on his blog and the flood of comments never disappoint. Yesterday's question was regarding read-only collections: Should a potentially read-only collection be derived from a read-only class, or should it have a ReadOnly property that indicates whether it's read-only.
I think it depends whether it's a class whose context is within an application versus a library. If it a class in an application, then the latter is fine. The behavior is known and expected. However, if it's in a library such as the FCL, I prefer the latter. Imagine if there was a read-only flag on ArrayList or Hashtable. Before adding/removing any members you'd always have to check it's ReadOnly property. Completely unnecessary. If it extended a ReadOnlyArrayList, then there is no question what it supports. If you need read-only, you type your param/variable/whatever as ReadOnlyArrayList; if you need to write to it, you type it as ArrayList.
Regardless, it's worth a read. Everyone has valid comments.