Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't the compiler convert var[] to object[] in c#?

There is no difference between these two lines, because the compiler, in the second line, understands that it is an array of type int.

var x = new int[] { 1, 2, 3 };   //Fine, x is int[]
var x = new [] { 1, 2, 3 };      //Fine, x is int[]

But why can't I do this with different types? Why doesn't the compiler convert my variable to type object?

var x = new object[] { 1, "df", 5 };   //Fine, x is object[]
var x = new [] { 1, "df", 5 };         //Error! "No best type found for implicity-typed-array"

EDIT:

Thanks for all your answers. But I still wonder, what are the pros and cons to make all expressions that the compiler can't convert to type object? (Because I use var notation which means that it can't be any type. I understand like this.) Why doesn't the compiler find the nearest type of the array members by going up the inheritance tree?

like image 809
Chuck Norris Avatar asked Jan 17 '12 08:01

Chuck Norris


2 Answers

The new [] notation is for saving you to type an explicit type of the array members (or allowing you to create arrays where its elements have an anonymous type), but its type inference is limited in that all elements must share the same type or be implicitly convertible to a common type shared by at least one member. See C# Specification, section 7.6.10.4:

An array creation expression of the third form is referred to as an implicitly typed array creation expression. It is similar to the second form, except that the element type of the array is not explicitly given, but determined as the best common type (§7.5.2.14) of the set of expressions in the array initializer.

The following are examples of implicitly typed array creation expressions:

var a = new[] { 1, 10, 100, 1000 };                       // int[]
var b = new[] { 1, 1.5, 2, 2.5 };                         // double[]
var c = new[,] { { "hello", null }, { "world", "!" } };   // string[,]
var d = new[] { 1, "one", 2, "two" };                     // Error

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.

Key point here is that the “best common type” can only be one of the types already present. As Damien_The_Unbeliever pointed out in a comment: “As Mr. Lippert is fond of pointing out around inference, whenever it's looking for a best common type, it will only return one of the types that is already present - it doesn't go hunting for the most-derived common ancestor.”.

Just because every array could be an object [] doesn't mean it should. From a compiler perspective that'd be a trivial last-resort choice, but a very counter-intuitive one for the developer, I guess.

like image 63
Joey Avatar answered Oct 03 '22 18:10

Joey


To expand on Joey's answer, consider this example:

interface IFoo { }
interface IBar { }

class A : IFoo, IBar { }
class B : IFoo, IBar { }

var array = new [] { new A(), new B() };

Both classes implement both interfaces (and also derive from object), so which type should be inferred for array?


To answer your comment, consider the case where A and B share only one interface:

interface IFoo { }

class A : IFoo { }
class B : IFoo { }

var array = new [] { new A(), new B() };

Both A and B share object as their base class, but it'd be unhelpful and mostly useless to infer this for the array type. One would expect it to be IFoo if anything, so it would violate the principle of least astonishment. However, this cannot be done consistently, as I've illustrated.

The safest and most consistent behaviour here is simply not to allow type inference.

like image 27
Will Vousden Avatar answered Oct 03 '22 19:10

Will Vousden