开发者

How is an array type declaration 'new int[10][]' evaluated?

a) How is an array type declaration new int[10][] evaluated? Is it evaluated

as (new int[10])[]

or as (new int[10][])

or …?

b) I’m not sure how to ask this: I know statement int[][] i = (new int[10])[] would give us compiler error. But assuming compiler wouldn’t report an error, what kind of array type would compiler create based on this statement?

EDITED:

I apologize for not being able to make my question more comprehensive. It’s one of those questions where something is bothering me and I’m not capable of crystallizing the question in my mind enough to make some sense of it. So I ask a half assed questions hoping that replies will help me with that. Anyways, it’s a pointless question but I would still like to explain what I’ve actually meant by it:

what prompted all of this was an article from MSDN where it said

Primary expressions are divided between array-creation-expressions and primary-no-array-creation-expressions. Treating array-creation-expression in this way, rather than listing it along with the other simple expression forms, enables the grammar to disallow potentially confusing code such as

object o = new int[3][1];

which would otherwise be interpreted as

object o = (new int[3])[1];

so that got me thinking that perhaps parenthesis around different parts of this array creation expression would give compiler different instructions on the type of array it should create.

Or to put it differently, I thought that perhaps compiler follows some build-in logic ( derived from some math field ) when interpreting array creation/initialization syntax. In other words, compiler didn’t build an array due to its developers giving it specific instruction such as “开发者_StackOverflow中文版when you see words new and name of some type and two pairs of square brackets ([] []), then follow these step-by-step instructions , which will tell you in full details what to do", but instead developers just build in some math related logic and from that compiler was capable deriving an array by itself, without developers giving it step by step instructions.

BTW – I do understand about jagged and rectangular arrays and how they are initialized etc ;)


Here's my article on how this works and what the design considerations were. Hopefully that answers your questions.

http://blogs.msdn.com/ericlippert/archive/2009/08/17/arrays-of-arrays.aspx

To attempt your specific questions:

How is an array type declaration "new int[10][]" evaluated?

That's not a type declaration. "class C {}" is a type declaration because it declares a new type. What you've got is an object creation expression.

It's evaluated by creating a new array with ten elements. Each element is a variable that can contain a reference to an array of integers. Each of those variables is initialized to null.

Is it evaluated as (new int[10])[] or as (new int[10][])

I don't understand the question. The former is not legal, and the latter is just the expression again in parentheses. So I guess the answer is that it is evaluated like the latter, since the two are the same, but I cannot help but think I'm missing the point of the question.

But assuming compiler wouldn’t report an error, what kind of array type would compiler create based on this statement?

If bishops were not restricted to moving on the diagonal, how would the queen's gambit play out differently?

The question does not provide enough information to answer the question. You have to say what the new rule for bishops is, not just what restriction is lifted. What would the new rule for bishops be? Once we know the new rule, then we can analyze how the game changes.

You haven't proposed what the specification would say, only what it would not say. That's not enough to determine the semantics of this proposed expression.


This is an array of size ten with members of type integer array, also known as a jagged array.

See this section of the C# tutorial on MSDN (specifically the Jagged Array section).


I think I can help answer your question a little bit. At least, I think I understand what you don't understand and can babble about it cogently for a moment.

When the compiler sees

int[][] a = new int[10][];

what it does it creates a one-dimensional array of length ten. Each element in this array is capable of holding a reference to an int[]. It then assigns a reference to this one-dimensional array of length ten to a. Therefore, if you understand what

int[] a = new int[10];

does you should now be able to understand what

int[][] a = new int[10][];

does. The former reserves ten contiguous slots that can hold int. The latter reserves ten continugous slots that can hold references to int[]. (We are leaving out additional details such as the Length property, etc.) So, if you imagine a fantasy syntax ArrayCreate(Type type, int length) the former is

int[] a = ArrayCreate(typeof(int), 10);

and the latter is

int[][] a = ArrayCreate(typeof(int[]), 10);

Now, I don't really understand what you're asking in your two questions labeled "a)" and "b)". Perhaps you can rephrase them in terms of this fantastical ArrayCreate syntax and I can try to answer them?

I hope that helps.


It would provide you with a vector with a width of 10 integer values and an indeterminate depth. The depth of each vector would be determined as each of the 10 indices is instantiated and populated.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜