I am wondering why this is valid :
object[] array = {"bla bla bla..", 23, true};
But these are not:
var array = {"bla bla bla..", 23, true };
var array2 = new [] {"bla bla bla..", 23, true };
In the second example why compiler cannot infer the array type according to values in the array initializer? It seems really easy to do, especially as compared to generic type inference.In order to define an object array why do I have to specify the array type explicitly ?
var array = new object[] { "bla bla bla..", 23, true };
Because the types in the array aren't specific object
- they are 3 different types that are all subclasses of object.
If you use a specific type that can be inferred in your array, the compiler will infer the type. For example, this is fine:
var arr = new[] {3, 4, 5}; // Will correctly infer int[]
Note that this is explicitly called out in 8.5.1 of the C# language spec, which states that for var
is subject to the following restrictions:
In the case of arrays, there is an example specified:
var y = {1, 2, 3}; // Error, array initializer not permitted
As for the new [] {"bla bla bla..", 23, true };
example, this is called out in 7.6.10. There, this example:
var d = new[] { 1, "one", 2, "two" }; // Error
Is said to be an error because:
The last expression causes a compile-time error because neither int nor string is implicitly convertible to the other, and so there is no best common type. An explicitly typed array creation expression must be used in this case, for example specifying the type to be object[]. Alternatively, one of the elements can be cast to a common base type, which would then become the inferred element type
I would say it's more of a safety feature. Sure, the compiler could always infer the least-common denominator, object
.
But consider the consequences: You make a mistake when initializing an array that you intended to be of a specific type: (here int
)
var array = {1, 2, 3, 4, 5, "6", 7, 8, 9};
Through a copy & paste error, you leave "6"
as a string. Instead of throwing a compiler error, you're left with an unintended object[]
, which could cause problems down the road where you were intending int[]
.
This isn't Python - I prefer to have explicit typing over a savings of a few characters.
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