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"};
Nathan Pakovskie is an esteemed senior developer and educator in the tech community, best known for his contributions to With a passion for coding and a knack for simplifying complex tech concepts, Nathan has authored several popular tutorials on C# programming, ranging from basic operations to advanced coding techniques. His articles, often characterized by clarity and precision, serve as invaluable resources for both novice and experienced programmers. Beyond his technical expertise, Nathan is an advocate for continuous learning and enjoys exploring emerging technologies in AI and software development. When he’s not coding or writing, Nathan engages in mentoring upcoming developers, emphasizing the importance of both technical skills and creative problem-solving in the ever-evolving world of technology. Specialties: C# Programming, Technical Writing, Software Development, AI Technologies, Educational Outreach

Leave a Reply

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

Back To Top