471,347 Members | 1,677 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,347 software developers and data experts.

Jagged and Multi-Dimensional Array Declaration Subtleties

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
Feb 4 '06 #1
4 2207
Alan Foxmore wrote:
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


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
Feb 5 '06 #2
"John Sun" <js***********@gmail.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Alan Foxmore wrote:
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
Feb 5 '06 #3
Alan Foxmore wrote:
"John Sun" <js***********@gmail.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Alan Foxmore wrote:
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.

Feb 5 '06 #4
Alan Foxmore <AF******@yahoo.com> wrote:

<snip>
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 5 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by kids | last post: by
3 posts views Thread by PaulW | last post: by
5 posts views Thread by bobwansink | last post: by
4 posts views Thread by Sahar | last post: by
reply views Thread by Ronak mishra | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.