1

Look into following code block:

string[] _array = new string[] { "a", "b", "c" };
List<string> _listArray = new List<string>() { "a", "b", "c" };

//Why Invalid?
_array.Add("e");
_array.Insert(4, "e");

//Allowed for list.
_listArray.Add("e");
_listArray.Insert(4, "e");

Since both Array and List implements almost same interface and use as collection of object then why Array dont have Add and Insert method

Community
  • 1
  • 1
Ankush Madankar
  • 3,689
  • 4
  • 40
  • 74
  • Array is a fixed size collection. If you can add or remove items to the Array the size will change and that violate the rules. – Ekk Dec 27 '13 at 07:56

1 Answers1

5

Almost is a key here.

Array has fixed size and you can't add or insert new elements. You can change existing ones using [] array-index operator.

The number of dimensions and the length of each dimension are established when the array instance is created. These values can't be changed during the lifetime of the instance.

from Array (C# Programming Guide)

And the fact that arrays implement certain interfaces with Add method, like ICollection does not change anything.

The key thing to be aware of when you cast an array to one of these interfaces is that members which add, insert, or remove elements throw NotSupportedException.

from Array Class (System)

MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263