Creating a C# array

Posted on 2006-04-05
Last Modified: 2008-01-09

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


Question by:qvee
    LVL 85

    Accepted Solution

    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" };
    LVL 1

    Assisted Solution

    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.
    LVL 1

    Assisted Solution

    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";
    LVL 10

    Expert Comment

    >>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.
    LVL 2

    Expert Comment

    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
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    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 {}:"

    LVL 3

    Expert Comment

    I usually do the same as Idle_Mind as well and find no point specifying both size and members.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    post4 challenge 28 70
    NotAlone Challenge 20 65
    how to open from address saved in DB? 26 114
    wordappend challenge 8 49
    I know it’s not a new topic to discuss and it has lots of online contents already available over the net. But Then I thought it would be useful to this site’s visitors and can have online repository on vim most commonly used commands. This post h…
    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now