David I. McIntosh David I. McIntosh - 1 month ago 4x
C# Question

Inconsistent initialization syntax

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:


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?

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


Suppose you have another method

void method(KeyValuePair<string,string> p) {}

calling it like method({"a","b"}); would result in ambiguity..

Remember {"a","b"} may also be KeyValuePair<> as here

var dict = new Dictionary<string, string>() { { "a", "b" } };