4

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 };
1
  • I guess it has something to do with derived classes, what should the compiler do in this case in, pick the base class or say its array of object? Commented Mar 17, 2014 at 21:58

3 Answers 3

6

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:

  • The local-variable-declaration cannot include multiple local-variable-declarators.
  • The local-variable-declarator must include a local-variable-initializer.
  • The local-variable-initializer must be an expression.
  • The initializer expression must have a compile-time type.
  • The initializer expression cannot refer to the declared variable itself

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

Sign up to request clarification or add additional context in comments.

3 Comments

it seems reasonable.but if I want to store a string,int and a boolean value into same array, the array type should be definitely object right? it can't be another type.because it's the only common type.what I'm wondering is why compiler cannot see this and infer the type for me? or is it possible to define store these types into another common type? I didn't try it but if I create a custom type and define implicit and explicit conversions for string, int and bool, can I use that type as common type? I'm thinking if that is possible than it might be a good reason.
@Selman22 Thats fine, but you can't infer the type. The spec specifically calls this out, as I mentioned. I'm sure they could have made it fallback to infer as object, but they decided not to. Only the compiler team could say definitively why this wasn't done, though I'm glad it wasn't, as it'd be dangerous, as anything "odd" would fallback to object.
@Selman22 - Instead of an object[] with a string, int, and boolean, you might try a Tuple or just write your own class.
2

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.

Comments

1

According to Microsoft

Array elements must all be the same type or implicitly convertible to the same type [...]. The best type must be one of the types present in the array expression. Elements will not be converted to a new type such as object.

See: Compiler Error CS0826

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.