Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inconsistent initialization syntax

Tags:

c#

In C#, the following is valid syntax, and this makes sense:

string[] v = {"a","b"};

But now consider this. Suppose we define

void method(string[] p) {...};

Then the following is not valid:

method({"a","b"});

which is inconsistent with the above.

Are there technical reasons that preclude the method call here from being valid syntax? That is, is there some ambiguity in interpretation of the syntax? Or is there some issue with memory management or persistence that makes it impossible to implement?

Edit: Eric Lippert's answer below is interesting - but it answers the "why" of design, which I wasn't actually asking (and indeed this question was originally closed because it appeared as though I was asking for his sort of answer).

L.B's answer may indeed not be the original "reason why" this syntax was no allowed (as per Eric L. comments). However, L.B's answer, as of now, is certainly a correct technical reason why such syntax could not be allowed, which is actually the question I was asking, so I choose to select L.B's answer as correct (though honestly it was a hard choice...).

like image 452
David I. McIntosh Avatar asked Jul 28 '26 12:07

David I. McIntosh


1 Answers

Short answer: it's an oddity of the grammar. I've always considered this to be a "wart". There are a number of ways you can look at this thing and say it is weird. It is weird that it is one of the few situations where:

T x = y;

and

T x; x = y;

are different. Or, another way to look at it is that it is weird that a local variable initializer is a context in which something that is not an expression can appear.

Or another way to look at it is it is really weird that this is the only situation in which a new array is created but "new" does not appear anywhere.

Or another way to look at it is it is really weird that arrays can be initialized like this, but no other collection constructs can. It makes arrays seem particularly special and important, even though they are often not the right tool for the job.

I think if we had to do it all over again, probably initializers would require new[] to their left.

Are there technical reasons that preclude the method call here from being valid syntax? That is, is there some ambiguity in interpretation of the syntax?

Not really. Don't read too much into this oddity. My advice is to avoid the syntax entirely; stick with proper collection initializers or array initializers. They have almost exactly the same syntax, but are legal anywhere an expression is legal.

Regarding the other answer, of L.B.: Though this answer does plausibly point out that there is a design issue here, it ignores the historical perspective. The feature being critiqued was created in C# 1.0, years before generic type inference (C# 2.0) or other braced collection initializers (C# 3.0) were added. So one cannot make a justification for the design choices of a 1.0 feature on the basis of some conflict with features that came years later. The C# design team tries to be forward looking, but they were not that forward looking!

like image 159
Eric Lippert Avatar answered Jul 30 '26 01:07

Eric Lippert