tags:

views:

120

answers:

1

The IList interface requires an Add method. Arrays implement this function but it simply throws a NotImplementedException. This seems like very bad design to me.

What were the designers thinking when they did this?

+4  A: 

ILists can be readonly - if in doubt the caller can test the IsFixedSize property before attempting to add or remove an element, or the IsReadOnly property before attempting to modify an element.

An array is a fixed-size IList.

It can be convenient to be able to treat an array as a list. One example is mocking a data access method that returns an IList - it can be mocked to simply return an array cast as an IList.

Joe
It's actually the `IsFixedSize` property that you'd need to check. `IsReadOnly` will be `false` for arrays because the existing elements can be modified. `IsFixedSize` will be `true` because elements can't be added or removed.
LukeH
Just to clarify the problem with fixed and read only: http://blogs.msdn.com/ericlippert/archive/2009/08/27/what-s-the-difference-between-fixed-and-fixed.aspx
Oliver
@Oliver: I'm a big fan of Eric Lippert's blog, but that article has absolutely nothing to do with the `IsFixedSize` or `IsReadOnly` properties.
LukeH