The C# language specification (7.6.10.4) says, that there are tree kinds of array creation expressions:
new non-array-type [ expression-list ] rank-specifiersopt array-initializeropt
new array-type array-initializer
new rank-specifier array-initializer
The third one is intended for implicitly typed arrays:
var foo = new[] { 1, 2, 3 };
The question: is there any weighty reason to forbid to set array size explicitly in case of implicitly typed array?
It looks like asymmetric behavior, comparing with this syntax:
var foo = new int[3] { 1, 2, 3 };
Update.
A little clarification. The only advantage for combination of explicitly set array size and array initializer I can see, is the compile-time check for initializer length. If I've declared the array of three int
s, the initializer must contain three int
s.
I think, the same advantage is true for the implicitly typed arrays. Of course, to use this advantage or not to use is the personal preference.
The rank specifier is not needed because it is already supplied by the number of elements in the initialization list.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With