The second syntax has been showed as non-doable by Tigran and Hilgarth.
Let's watch the first syntax:
var rgb = (1.0f, 1.0f, 1.0f);
What happen if you don't want to use the Tuple
class, because instead you want to use the MyTuple
class (that perhaps has the advantage of being IEnumerable<object>
, something quite useful!)? Clearly that syntax won't help. You would have to put the MyTuple
class somewhere...
MyTuple<float, float, float> rgb = (1.0f, 1.0f, 1.0f);
or
var rgb = new MyTuple<float, float, float>(1.0f, 1.0f, 1.0f);
now the advantage of this new shorthand syntax isn't present anymore, because somewhere you have to put the MyTuple<float, float, float>
.
Note that there isn't any single collection initializer that simply "auto-discovers" everything.
var myVar = new List<int> { 1, 2, 3 };
Here the fact that we are speaking of a List<int>
is quite clear :-)
Even the array initializer, that is a little "special", isn't implicit...
int[] myVar = { 1, 2, 3 };
var myVar = new[] { 1, 2, 3 };
var myVar = new int[] { 1, 2, 3 };
are all valid, but the fact that we are speaking of an array is always explicit (there is always a []
)
var myVar = { 1, 2, 3 };
isn't valid :-) And the array has the "advantage" of being a primitive construct (arrays are supported directly by the IL language, while all the other collections are built on top of other .NET libraries and/or the arrays)