0

I'm having trouble understanding this problem with creating a jagged array in C#. If you look at the code below, it compiles fine. But if I were to take the 2 lines that are assigning values to the array "places" and move them from within the method to within the class itself, the compiler starts complaining with a lot of strange errors.

At first I thought that its because of the usage of the 'new' keyword within a class (a class being just a definition whereas 'new' refers to an instance... you can't have an instance within a definition, can you?). But then I noticed the usage of the 'new' keyword in the initialization of "places" was OK even though it was initialized within the class. Please explain.

public class Place
{
string[][] places = new string[2][];
   public void enumerate()
   {
     places[0] = new string[] { "Canada", "United States" };
     places[1] = new string[] { "Calgary", "Edmonton", "Toronto" };

     Console.WriteLine(places[0][1]);
   }
}

EDIT: to be explicit, the error is produced when running

public class Place
{
string[][] places = new string[2][];
places[0] = new string[] { "Canada", "United States" };
places[1] = new string[] { "Calgary", "Edmonton", "Toronto" };
   public void enumerate()
   {
     Console.WriteLine(places[0][1]);
   }
}

errors received "array size cannot be specified in a variable declaration (try initializing with a "new" expression)" "invalid tokens =, {, in class" "namespace cannot directly contain members such as fields or methods"

7
  • Why would you use a jagged array for this? It would be better served as class objects. Commented Aug 22, 2013 at 19:33
  • I'm not sure what you mean. Ignore the meaning of the values used. It is a completely meaningless array. I'm just wondering why it won't compile. Commented Aug 22, 2013 at 19:37
  • How does that not compile? It did for me output = United States. Commented Aug 22, 2013 at 19:53
  • DonA. I appreciate you looking through this but if you re-read my post I said it DOES compile, however, it DOESN'T when the 2 lines are moved into the class, rather than the method. Commented Aug 22, 2013 at 20:20
  • Right, I mean what errors? I don't get any. Can you show us how you initialize this class? Commented Aug 22, 2013 at 20:52

1 Answer 1

3

Of course you can initialize an jagged array, but the syntax is slightly different:

public class Place
{
    private string[][] places = new string[2][]
    {
        new string[] { "Canada", "United States" },
        new string[] { "Calgary", "Edmonton", "Toronto" },
    };

    public void enumerate()
    {
        Console.WriteLine(places[0][1]);
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Well, that solves that. Thank you. I'm still fuzzy on why this is a special case with arrays? And i'm not familiar with the syntax you used (encapsulating an array).
It is simply the syntax for array declarations and initialization, some examples here
Thanks Alberto. I suppose that's just the way its done with arrays and I will get used to the syntax.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.