I'm writing an array wrapper class that implements IList<T>
. I’m unsure of what to return for IList<T>.IsReadOnly
(inherited from ICollection<T>
), though.
My class disallows insertion and removal. It does allow modifying items via the this[int].set
property.
The MSDN states that
A collection that is read-only does not allow the addition, removal, or modification of elements after the collection is created.
For my class, this seems to imply that I have to return true
but in my eyes this renders the property a bitcompletely useless: as far as I can see, the use of this method is as follows:
The clients handle an arbitrary IList
and need to insert an element into it, if at all possible. They can do this by just calling Insert
and catching the resulting NotSupportedException
– and for various reasons, this may not be desirable. So instead of provoking an exception, the clients can just test the IsReadOnly
property beforehand.
But the result of this property will be wrong because it mixes modifiability of the collection with modifiability of its contents – which are completely unrelated matters!
To be sure, there’s the IList.IsFixedSize
property but this is a separate type (IList<T>
does not extend IList
). What should I do? Also implement IList
(I really don’t like this alternative)? Do something else?