Array creation must have array size or array initializer

Array creation must have array size or array initializer

Array creation must have array size or array initializer error is caused by a line similar to the following:

string[] strMyArray = new string[];

And the problem with this line is that you create a new array but you don’t define a size. When you create an array you have to either define a size for the array, or declare its elements.

An example of defining the size of the array (in this case 4 elements) can be seen below:

string[] strMyArray = new string[3];

Remember that the count starts from 0, so the first element of the array is 0, the second is 1, the third is 2 and the fourth is 3.

For a multidimensional array, here’s an example:

string[] strMyArray = new string[3,5];

If you don’t want to specify the size of the array, you must specify the elements of the array at the same time of declaring it:


string[] strMyArray = {"Chrysler", "Dodge", "Cadillac", "GM"};

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top