This code:
int[] myArr = { 1, 2 };
myArr.Add(3);
throws on Build:
error CS1061: 'System.Array' does not contain a definition for 'Add' and no extension method 'Add' accepting a first argument of type 'System.Array' could be found (are you missing a using directive or an assembly reference?)
IList
interface has the Add()
method, but why the Array does not implement it?
UPDATE: I see from the answers that it DOES implement it explicitly, OK, I get it, thank you, I should better stick to the question:
Why Array
does not actually provide Add()
, OR, better, why did it have to implement IList
in the first place? Instead of implementing IList
, it could be another interface (e.g. IArray
) which could have ONLY the useful for Array members of IList
-e.g. IsFixedSize
, IsReadOnly
, IndexOf()
... just a thought.
Why Array does not actually provide Add()?
Array has fixed size, so you cannot add new element(s).
Why did it have to implement IList in the first place?
Array is accessed by index and IList accommodate this index, which is why Array implements IList.
For reference: Why array implements IList?
It does provide Add, but by throwing a
NotSupportedException
(see MSDN), because the size of an array is fixed.The reason why you get a compilation error, instead, is because the interface is implemented explicitly, so if you want to call the method you need to cast to
IList
. See this C# guide about explicit interface implementation.IList
is implemented in three different categories:Obviously the
Array
type is a fixed size implementation ofIList
. The reason why you cannot access theAdd()
method from anArray
is because the method is implemented explicitly:This means you need to cast your array to an
IList
before you're able to use theAdd
method (even though it will throw an non-supported exception).You might say this is a bad design, and many people would agree with you on that.
Read more on explicitly defined interfaces: https://msdn.microsoft.com/en-us/library/aa288461(v=vs.71).aspx
Yes, it seems that it should have been a better design if
System.Array
had implementedIReadOnlyList
or alike interface. However,IReadOnlyList<T>
appeared in .Net 4.5 whileSystem.Array
stays from the initial .Net 1.0. Microsoft, IMHO, did their best and hidAdd
via explicit interface implementationhttp://referencesource.microsoft.com/#mscorlib/system/array.cs,156e066ecc4ccedf
So you can't do
but you can insist on using
Add
(and getNotSupportedException
thrown) viaOr even
Though a class implementing an interface must implement all members of the interface, it can implement them explicitly:
If you implement the method this way, it won't be visible on instances of
MyList<T>
:So if you have an
int[]
you could do that:It will compile, but at runtime a
NotSupportedException
will be thrown because arrays have fixed size and you cannot add a new element to an array as it's size is determined on initialization (new int[0]
).According to msdn:
Array Class - see explicit interface implementation section
Array
has this method. To call this method you should explicitly cast toIList
. This method can't be called, because array has fixed size and this size can't be dynamically changed.