Why does array implement ilist
NotSupportedException: Collection was of a fixed size" and occurred because array can not increase its capacity dynamically. Its capacity is defined during creation of array object. For example, a Count ends up calling the Array. Length under-the-hood, since it's casted to ICollection and the array's implementation returns Length.
Without this, the Linq2Objects engine would not have special treatment for arrays and perform horribly, or they'd need to double the code adding special-case treatment for arrays like they do for IList. They must've opted to make array implement IList instead. See the definition of System. Array class public abstract class Array : IList , Asked By: oleksii.
Answered By: CodesInChaos. Answered By: Brian Rasmussen. The GetInterfaceMap call fails for a concrete array type with "Interface not found". This is quacks-like-a-duck typing. It is the same kind of typing that creates the illusion that every value type derives from ValueType which derives from Object. Both the compiler and the CLR have special knowledge of array types, just as they do of value types. And emits the castclass IL instruction.
It has built-in knowledge of the otherwise hidden System. SZArrayHelper class, a wrapper that actually implements these interfaces. Which it doesn't do explicitly like everybody claims, the Count property you asked about looks like this:. Yes, you can certainly call that comment "breaking the rules" : It is otherwise darned handy. Search for "IList" to see where the type substitution takes place. Just about any core. NET type gets substituted there.
Itself a substitution, COM doesn't support generic types. Well, that was a look at what happens behind the curtain. It can be very uncomfortable, strange and unfamiliar seas with dragons living at the end of the map. It can be very useful to make the Earth flat and model a different image of what's really going on in managed code.
Mapping it to everybody favorite answer is comfortable that way. Which doesn't work so well for value types don't mutate a struct! The GetInterfaceMap method failure is the only leak in the abstraction that I can think of. Why System.
Array has fixed size, so you cannot add new element s. For reference: Why array implements IList? Add 3 ; Or even if! So perhaps your real question is "Why is there no interface for constant collections with indexers? There are no readonly interfaces for collections either. And I'm missing those even more than a constant sized with indexers interface. IMO there should be several more generic collection interfaces depending on the features of a collection.
And the names should have been different too, List for something with an indexer is really stupid IMO. I think the current collection interfaces are bad design. But since they have properties telling you which methods are valid and this is part of the contract of these methods , it doesn't break the substitution principle. The remarks section of the documentation for IList says:. IList is a descendant of the ICollection interface and is the base interface of all non-generic lists.
IList implementations fall into three categories: read-only, fixed-size, and variable-size. A read-only IList cannot be modified. A fixed-size IList does not allow the addition or removal of elements, but it allows the modification of existing elements. A variable-size IList allows the addition, removal, and modification of elements.
Obviously, arrays fall into the fixed-size category, so by the definition of the interface it makes sense. Because not all IList s are mutable see IList.
IsFixedSize and IList. IsReadOnly , and arrays certainly behave like fixed-size lists. If your question is really "why does it implement a non-generic interface", then the answer is that these were around before generics came along.
It's a legacy that we have from the times when it wasn't clear how to deal with read only collections and whether or not Array is read only. IsReadOnly flag means that collection can't be changed at all and IsFixedSize means that collection does allow modification, but not adding or removal of items.
At the time of. Net 4. Here is a great blog post describing the details: Read only collections in. Definition of IList interface is "Represents a non-generic collection of objects that can be individually accessed by index. Array completely satisfies this definition, so must implement the interface. Exception when calling Add method is "System. NotSupportedException: Collection was of a fixed size" and occurred because array can not increase its capacity dynamically.
0コメント