Creating a C# array


I'm trying to use the following to create a string array:

string [] strArray = new string [12]  {"hello","world"} ;

Accordng to my references, this is legal, but the compiler burps with the following message:

Invalid rank specifier: expected ',' or ']' (CS0178)

Creating the array and then assigning the text works as expected, however


Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike TomlinsonMiddle School Assistant TeacherCommented:
If you're giving it values to assign then I don't think you can specify the initial upper bound of 12.  It determines the size of the array based on the number of values between the braces {}:

        private void button1_Click(object sender, EventArgs e)
            string[] strArray = new string[] { "hello", "world" };

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
string [] strArray ;
strArray = = new string [2] {"Hello", "World"}

The size of array specified and the number of string literals need to match for this code to get compiled.
If you need the string array size to 12 but you want to set values to the first and second items of the string array, the following code will be ok.

                string[] strArray = new string[12];
      strArray[0] = "hello";
      strArray[1] = "world";
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

>>string [] strArray = new string [12]  {"hello","world"} ;

The compiler expects to find the 12 elements in the array....when u just have 2 should be

string [] strArray = new string [2]  {"hello","world"} ;

if you are not sure on the number of elements your array contain...u might want to use this...
string[] strArray = new string[12];
strArray[0] = "hello";
strArray[1] = "world";

and the rest of the its items are automatically initialized to the default initial value for the array type if the array is not initialized at the time it is declared.
what findsukesh & moe2san are saying is correct however they failed explain why .NET behaves that way

when you are declaring and initilizing an array simoultaneously .NET assumes you are giving data to all its array elements so if it finds that you have not provided in that way, then it throws an compiler exception.

So if you want the array to be of size 12 but initilize only 2 array elements then what moe2san has given should do. or the below one will also do.

string [] strArray = new string [12]  {"hello","world", "", "", "", "", "", "","", "", "",} ;

Hope it solved your problem
Mike TomlinsonMiddle School Assistant TeacherCommented:
Isn't that I said in the very first comment of this thread?...

   "It determines the size of the array based on the number of values between the braces {}:"

I usually do the same as Idle_Mind as well and find no point specifying both size and members.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.