We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

Creating a C# array

qvee
qvee asked
on
Medium Priority
1,473 Views
Last Modified: 2008-01-09
Hello,,

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

Thanks.

Comment
Watch Question

High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009
Commented:
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" };
            MessageBox.Show(strArray.Length.ToString());
        }

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
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.
Commented:
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";
Jagadeesh MBig Data and Splunk Architect

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

The compiler expects to find the 12 elements in the array....when u just have 2 elements...it 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
~BAS
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
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 {}:"

=)

Commented:
I usually do the same as Idle_Mind as well and find no point specifying both size and members.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.