Jagged and Multi-Dimensional Array Declaration Subtleties

  • Thread starter Thread starter Alan Foxmore
  • Start date Start date
A

Alan Foxmore

Hi everyone,

I'm new to C# and I was hoping I could get some clarification on the
syntax for jagged and multidimensional arrays.

Here is my question:

The following syntax is correct for declaring a multi-dimensional
array:

int[,] v = {{ 3, 6, 4 },
{ 2, 8, 1 }};

If I declare a similar jagged array, however, I have to do this
instead:

int[][] w = {new int[] { 3, 6, 4 },
new int[] { 2, 8, 1 }};

Specifically, the "new int[]" declarations are absolutely necessary to
avoid a compiler error. If you remove the "new int[]" from the
declaration you get this compiler error:

"Array initializers can only be used in a variable or
field initializer. Try using a new expression instead."

I know that v is a multi-dimensional array and w is an "array of int
array". So I understand the difference in semantics. But I can't
articulate *why* there is a difference in the syntax.

Thanks very much.
 
Alan said:
Hi everyone,

I'm new to C# and I was hoping I could get some clarification on the
syntax for jagged and multidimensional arrays.

Here is my question:

The following syntax is correct for declaring a multi-dimensional
array:

int[,] v = {{ 3, 6, 4 },
{ 2, 8, 1 }};

If I declare a similar jagged array, however, I have to do this
instead:

int[][] w = {new int[] { 3, 6, 4 },
new int[] { 2, 8, 1 }};

Specifically, the "new int[]" declarations are absolutely necessary to
avoid a compiler error. If you remove the "new int[]" from the
declaration you get this compiler error:

"Array initializers can only be used in a variable or
field initializer. Try using a new expression instead."

I know that v is a multi-dimensional array and w is an "array of int
array". So I understand the difference in semantics. But I can't
articulate *why* there is a difference in the syntax.

Thanks very much.

I think you have a pretty clear understanding of the difference, that's
just how people in MS define the syntax.

You can look

int[][] w = {new int[] { 3, 6, 4 },
new int[] { 2, 8, 1 }};

this way:
int[][] w=new int[2][];
w[0]=new int[] { 3, 6, 4 }
w[1]=new int[] { 2, 8, 1 }

Then , you should know the new is required here.

Programming Language grammar is kind of similar with normal grammar, it
largely depends on how people define them.

Jianwei
 
John Sun said:
Alan said:
Hi everyone,

I'm new to C# and I was hoping I could get some clarification on the
syntax for jagged and multidimensional arrays.

Here is my question:

The following syntax is correct for declaring a multi-dimensional
array:

int[,] v = {{ 3, 6, 4 },
{ 2, 8, 1 }};

If I declare a similar jagged array, however, I have to do this
instead:

int[][] w = {new int[] { 3, 6, 4 },
new int[] { 2, 8, 1 }};

Specifically, the "new int[]" declarations are absolutely necessary
to avoid a compiler error. If you remove the "new int[]" from the
declaration you get this compiler error:

"Array initializers can only be used in a variable or
field initializer. Try using a new expression instead."

I know that v is a multi-dimensional array and w is an "array of int
array". So I understand the difference in semantics. But I can't
articulate *why* there is a difference in the syntax.

I think you have a pretty clear understanding of the difference,
that's just how people in MS define the syntax.

You can look

int[][] w = {new int[] { 3, 6, 4 },
new int[] { 2, 8, 1 }};

this way:
int[][] w=new int[2][];
w[0]=new int[] { 3, 6, 4 }
w[1]=new int[] { 2, 8, 1 }

Then , you should know the new is required here.

Programming Language grammar is kind of similar with normal grammar,
it largely depends on how people define them.




Well, Thanks for the response but I was hoping for a more rigorous,
formal explanation. In your first example you're using an initializer
whereas in the second you're using assignment.

I may be wrong but I feel the real explanation is more precise than,
"...just how people in MS define the syntax".
 
Alan said:
John Sun said:
Alan said:
Hi everyone,

I'm new to C# and I was hoping I could get some clarification on the
syntax for jagged and multidimensional arrays.

Here is my question:

The following syntax is correct for declaring a multi-dimensional
array:

int[,] v = {{ 3, 6, 4 },
{ 2, 8, 1 }};

If I declare a similar jagged array, however, I have to do this
instead:

int[][] w = {new int[] { 3, 6, 4 },
new int[] { 2, 8, 1 }};

Specifically, the "new int[]" declarations are absolutely necessary
to avoid a compiler error. If you remove the "new int[]" from the
declaration you get this compiler error:

"Array initializers can only be used in a variable or
field initializer. Try using a new expression instead."

I know that v is a multi-dimensional array and w is an "array of int
array". So I understand the difference in semantics. But I can't
articulate *why* there is a difference in the syntax.
I think you have a pretty clear understanding of the difference,
that's just how people in MS define the syntax.

You can look

int[][] w = {new int[] { 3, 6, 4 },
new int[] { 2, 8, 1 }};

this way:
int[][] w=new int[2][];
w[0]=new int[] { 3, 6, 4 }
w[1]=new int[] { 2, 8, 1 }

Then , you should know the new is required here.

Programming Language grammar is kind of similar with normal grammar,
it largely depends on how people define them.




Well, Thanks for the response but I was hoping for a more rigorous,
formal explanation. In your first example you're using an initializer
whereas in the second you're using assignment.

I may be wrong but I feel the real explanation is more precise than,
"...just how people in MS define the syntax".
Hi, buddy,

If I were you , I mean, if I am really serious about what the difference
is , I will write some sample code, compile it, and use
MSIL Disassembler (Ildasm.exe) to check whether they are compiled
differently.
 
Specifically, the "new int[]" declarations are absolutely necessary to
avoid a compiler error. If you remove the "new int[]" from the
declaration you get this compiler error:

"Array initializers can only be used in a variable or
field initializer. Try using a new expression instead."

I know that v is a multi-dimensional array and w is an "array of int
array". So I understand the difference in semantics. But I can't
articulate *why* there is a difference in the syntax.

Well, the syntax specifies exactly what's in the array in each case.
Apart from anything else, it makes it absolutely obvious whether you're
dealing with a multi-dimensional array or a jagged array.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top