Connecting Tech Pros Worldwide Forums | Help | Site Map

Jagged and Multi-Dimensional Array Declaration Subtleties

Alan Foxmore
Guest
 
Posts: n/a
#1: Feb 4 '06
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



John Sun
Guest
 
Posts: n/a
#2: Feb 5 '06

re: Jagged and Multi-Dimensional Array Declaration Subtleties


Alan Foxmore wrote:[color=blue]
> 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
>
>[/color]

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
Alan Foxmore
Guest
 
Posts: n/a
#3: Feb 5 '06

re: Jagged and Multi-Dimensional Array Declaration Subtleties


"John Sun" <jsunnewsgroup@gmail.com> wrote in message
news:%23vsb5YeKGHA.3272@tk2msftngp13.phx.gbl...[color=blue]
> Alan Foxmore wrote:[color=green]
>> 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.
>>
>>
>>[/color]
>
> 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.[/color]




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


John Sun
Guest
 
Posts: n/a
#4: Feb 5 '06

re: Jagged and Multi-Dimensional Array Declaration Subtleties


Alan Foxmore wrote:[color=blue]
> "John Sun" <jsunnewsgroup@gmail.com> wrote in message
> news:%23vsb5YeKGHA.3272@tk2msftngp13.phx.gbl...[color=green]
>> Alan Foxmore wrote:[color=darkred]
>>> 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.
>>>
>>>
>>>[/color]
>> 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.[/color]
>
>
>
>
> 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".[/color]
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.

Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#5: Feb 5 '06

re: Jagged and Multi-Dimensional Array Declaration Subtleties


Alan Foxmore <AFoxmore@yahoo.com> wrote:

<snip>
[color=blue]
> 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.[/color]

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 - <skeet@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
Closed Thread