Omitting c# new from jagged array initialization

旧城冷巷雨未停 提交于 2019-12-10 17:11:00

问题


From: http://msdn.microsoft.com/en-us/library/2s05feca.aspx

Notice that you cannot omit the new operator from the elements initialization because there is no default initialization for the elements:

int[][] jaggedArray3 = 
{
    new int[] {1,3,5,7,9},
    new int[] {0,2,4,6},
    new int[] {11,22}
};

What does it mean?

Why is it ok to omit new in:

int[]    arrSimp = { 1, 2, 3 };
int[,]   arrMult = { { 1, 1 }, { 2, 2 }, { 3, 3 } };

but not possible in:

int[][,] arrJagg = {new int[,] { { 1, 1} }, new int[,] { { 2, 2 } }, new int[,] { { 3, 3 } } };

回答1:


First off, what a coincidence, an aspect of your question is the subject of my blog today:

http://ericlippert.com/2013/01/24/five-dollar-words-for-programmers-elision/

You've discovered a small "wart" in the way C# classifies expressions. As it turns out, the array initializer syntax {1, 2, 3} is not an expression. Rather, it is a syntactic unit that can only be used as part of another expression:

new[] { 1, 2, 3 }
new int[] { 1, 2, 3 }
new int[3] { 1, 2, 3 }
new int[,] { { 1, 2, 3 } }
... and so on

or as part of a collection initializer:

new List<int> { 1, 2, 3 }

or in a variable declaration:

int[] x = { 1, 2, 3 };

It is not legal to use the array initializer syntax in any other context in which an expression is expected. For example:

int[] x;
x = { 1, 2, 3 }; 

is not legal.

It's just an odd corner case of the C# language. There's no deeper meaning to the inconsistency you've discovered.




回答2:


In essence the answer is "because they (meaning the language designers) choose not to.To quote from Eric Lippert:

The same reason why every unimplemented feature is not implemented: features are unimplemented by default. In order to become implemented a feature must be (1) thought of, (2) designed, (3) specified, (4) implemented, (5) tested, (6) documented and (7) shipped.

More technically there is a good reason to it and that's the definition of jagged arrays compared to 1-dimension and multi-dimension arrays.

A one or more dimension arrays can be expressed in plain English as a X dimension array of T where a jagged array has to be expressed as an Array of arrays of T. In the second case, there is a loose coupling between the inner array and the outer arary. That is, you can assign a new array to a position within the outer array whereas a x dimension array is fixed.

Now that we know that Jagged arrays are very different from multi-dimensional arrays in their implementation, we can also assume why there is a different level of integrated support for the 2. It's certainly not impossible to add support, just a question of demand and time.

(as a teaser, why only add support for jagged arrays? how about your own custom types?)



来源:https://stackoverflow.com/questions/14492279/omitting-c-sharp-new-from-jagged-array-initialization

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!