Take the 2-minute tour ×

Why is it that within C# you can't add objects to an array unless you declare the length of the array or add the item straight away? For example I could not simply do this

foreach(object item in array)

I would have to create a list and convert it to an array after the loop.

I do not want to know how to do, I want to have a logical explination of WHY arrays are fixed lengths but other collections aren't (List, ListArray)

share|improve this question
Because you can't modify collection which you are enumerating in foreach loop –  Sergey Berezovskiy Jul 9 '13 at 10:24
Array contains no method Add so you must be using a List like object already... –  dav_i Jul 9 '13 at 10:27
Isn't that just duplicating your array items? –  Sayse Jul 9 '13 at 10:27
This question start with the nonsensical assumption that arrays have an Add method. You cannot reasonably expect an answer based on a nonsensical assumption. –  Hans Passant Jul 9 '13 at 10:28
1 - Infinite loop, 2 - Array has fixed bounds, 3 - Cannot modify an enumerable while enumerating –  Tarik Jul 9 '13 at 10:46

6 Answers 6

up vote 4 down vote accepted

Arrays are not resizable because that's how memory works. Getting into how memory allocation works exactly in .NET is a little much for an SO answer, you can read all about it elsewhere if you want, but one aspect is basically universal across every platform: when you have a piece of memory allocated for something, the next address after that something might be part of some other thing.

So you can't just expand a piece of memory and hope nothing goes wrong, you'd have to test that you can expand it and have a plan B for when you can't - that plan B will be allocating a new block of memory, copying the old thing into it, and deallocating the old block.

To avoid reallocating and copying many times, it's common practice to make the new block twice as big as the old block, so if you add n items, you will only reallocate&copy O(log n) times, keeping the amortized time for adding a new item to a dynamically-grown array down to O(1). So constant time, great - not really. It's still more time than if you didn't have to reallocate, and in fact even every add that doesn't cause a grow has a little overhead to make sure that no grow is necessary yet.

So, arrays that don't grow are a lot simpler, a little faster, and waste less memory. So in C# (and many other languages, such as Java and C++) you get to choose: do you want to use a growable "array-like-thing", or a non-growable one? There is a trade-off there, and these languages choose not to take that decision for you.

share|improve this answer
I actually like this answer very much, although it is not too helpful for someone who, as it appears to me, has still to understand some basics of programming. A little hint for those who are motivated by this to do some research: Look at the idea of Arrays and at the implementations of List and ArrayList. You'll find it all makes sense. –  Romiox Jul 9 '13 at 11:24
I know how arrays work.. It was just a poorly worded question that people misunderstood. –  Hello World Jul 9 '13 at 11:47
@HelloWorld it happens, I almost never word my questions such that other people understand what I'm trying to ask.. asking a question is harder than answering one :) –  harold Jul 9 '13 at 11:49
Oh a side note, thank you very much Harold that is exactly what I was looking for. –  Hello World Jul 9 '13 at 11:53

Answer to the original (before the answer was edited):

Because a foreach loop does not dynamically change its end condition, while you are changing dynamically the object it is iterating over.

In your case (if it would be possible) it would never end, since in every loop one item is added to the array.

You can use a normal for loop for this:

int orgSize = array.Count
for (var i = 0; i < orgSize; i++)

Answer for the updated question: WHY arrays are fixed lengths but other collections aren't (List, ListArray):

Arrays are fixed length probably because of historical reasons and keeping backward compatibility with C/C++. These 'normal' array don't have Add, Count, neither are they dynamic. Therefor it is much easier to use List/ListArray etc, although this come to a cost, like maybe some performance and in all cases more storage needed, because administration for the instantiation is needed. However, if this is not an issue, I prefer to use List/ListArray/Dictionary kind of types because of the improved usability/features.

share|improve this answer
Adding an element to the same array where you are looping on? Not sure what the end result will be. –  Steve Jul 9 '13 at 10:30
Probably an infinite loop, since you're using the length of the array as the loop condition... –  Cody Gray Jul 9 '13 at 10:31
@Everyone above: I fixed the problem. –  Michel Keijzers Jul 9 '13 at 10:34
Yea it's OK now –  harold Jul 9 '13 at 10:34
arrays don't have an add method on them :s? –  Hello World Jul 9 '13 at 10:37


  1. Arrays are fixed in length from the time they are declared, you can neither add nor remove items. You can create a new array with greater length and copy from one to the other, but you cannot change the length of an array dynamically. You may want a List<> or some other collection type.

  2. You can't change any collection while you're iterating over it (List, Dictionay, ArrayList etc), it's a limitation the language/library designers put in place. Allowing it would involve a huge amount of work, and would have performance implications that would hamper performance of every foreach loop, not just the ones where you change the collection while iterating.

So tl;dr you can't because that's the way it's designed

Hope this helps.

share|improve this answer
This is -sort- of what I was looking for. –  Hello World Jul 9 '13 at 10:45
@HelloWorld what are you looking for? A rationale for why arrays are fixed length? –  harold Jul 9 '13 at 10:46
Yes, essentially. –  Hello World Jul 9 '13 at 10:47

You could also just go the old fashioned route and use a for-loop. That doesn't know about the iterator- and collection change problems, and you don't need that temporary list thingy (seems rather inefficient).

share|improve this answer

Arrays have a fixed length. you can nevertheless use a loop to populate them if you know the required size beforehand:

var sourceArray = new string[100];
/* populate source */

var myArray = new string[sourceArray.Length];

for (var i = 0; i < sourceArray.Length; i++)
  myArray[i] = sourceArray[i]; 
share|improve this answer

Actually, your question is not clear, or does not quite make sense. If you're trying to populate an existing array (with a defined size, but no values), you could do something like this:

var myArray = new string[5];

  myArray[i] = "Some value here..."; 

Adding existing items to the array itself makes no sense however.

Note: The Add() method exists for List etc., but there, it typicallye means "Append this element to the end of my collection".

An array has a defined length, which can not change, so you can not append to it - only change existing elements.

share|improve this answer

Your Answer


