0

I'm trying to make an array that holds 6 strings in one row, and many other rows after that. This is my code:

string[, , , , , ,] conj = new string[,,,,,,] {
        {"aimer", "aime", "aimes", "aime", "aimons", "aimez", "aiment"},
};

I'm getting the error "A nested array initializer is expected". I've never made an array this big before, and I couldn't find anything searching Google about why I'm getting this error and what I need to do to fix this.

Thanks for the help!

5
  • 1
    I think it's worth reading some quick overview about language syntax before start writing code. It took just 10 seconds to find this MSDN article about arrays in C#. msdn.microsoft.com/en-Us/library/0a7fscd0.aspx Commented Nov 8, 2014 at 0:27
  • 2
    This question appears to be off-topic because it is about a fundamental language feature (arrays) which is a subject too broad for a Stack Overflow answer. This site is not a substitute for a good book, tutorial or class at school. Commented Nov 8, 2014 at 0:29
  • 1
    If this was the syntax to do that, you'd be in trouble for large arrays, and in even deeper trouble for arrays whose size is not a compile time constant. Commented Nov 8, 2014 at 0:55
  • If I've made some mistake in syntax then sorry! I've been programming in C# for roughly 2 years and took classes. I posted a question on here because I couldn't find anything on Google... I was hoping that if it is just a syntax error, someone could help me fix it... Commented Nov 8, 2014 at 3:52
  • I'm not that great at wording things, so the question was possibly misread. That one line is just for one row with six columns, and then i'm going to have rows after that with the same six columns. Yeah.. No need to be mean guys Commented Nov 8, 2014 at 4:04

2 Answers 2

15

You are trying to initialize a multi-dimensional rectangular array (7 dimensions!).

So...

// 1D array containing 2 elements:
int[]  r1d = { 1 , 2 , } ;

// 2x3 array containing 6 elements:
int[,] r2d = {
               { 1 , 2 , 3 , } ,
               { 4 , 5 , 6 , } ,
             } ;

// a 2x3x4 array
int[,,] r3d = {
                {
                  {  1 ,  2 ,  3 ,  4 , } ,
                  {  5 ,  6 ,  7 ,  8 , } ,
                  {  9 , 10 , 11 , 12 , } ,
                } ,
                {
                  { 13 , 14 , 15 , 16 , } ,
                  { 17 , 18 , 19 , 20 , } ,
                  { 21 , 22 , 23 , 24 , } ,
                } ,
              } ;

One might see a pattern developing here. You should be able to take it from here (hint: you're going to have curly braces nested 7 deep).

Note that each the initializers must all be of the same rank, lest the compiler get upset. For instance, if you say:

int[,,] r3d = {
                {
                  {  1 ,  2 ,  3 ,  4 , } ,
                  {  5 ,  6 ,  7 ,  8 , } ,
                  {  9 , 10 , 11 , 12 , } ,
                } ,
                {
                  { 13 , 14 , 15 , 16 , } ,
                  { 17 , 18 , 19 , 20 , } ,
                //{ 21 , 22 , 23 , 24 , } ,
                } ,
              } ;

The compiler whines and says, An array initializer of length '3' is expected. That's because the initializer for x3d[0,1] the initializers are inconsistent.

Sign up to request clarification or add additional context in comments.

2 Comments

Oh!! I think I've just had a bit of a misunderstanding of arrays. I thought the commas were for element numbers from looking at another array I had coded... talk about brain fart. Thank you for your help! I have fixed the issue :) I just needed one comma, as it is a 2d array. @Nicholas
C# has two different array notation. With respect to two-dimensional array, a[x][y] is a jagged or sparse array, whilst b[x,y] is a rectangular or contiguous array. Strictly speaking, a[x][y] is an array of arrays. It has a fixed number of rows, but each row element a[x] is itself an array, allocated individually. Consequently, each row can have varying numbers of columns. a[x,y], on the other hand, is a single, contiguous, block of memory with a fixed number of rows and a fixed number of columns. The model, of course, extends to however many dimensions you give the array.
0

Your array definition string[, , , , , ,] shouldn't have any commas. Do string[] instead.

An array [] is a list of things. What you had is called a rectangular array (although that definition may not longer apply with that many dimensions). With a simpler case: [,], (which really is a rectangular array) what you get is more analogous to a "grid" of items rather than a list.

Comments

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.