Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type inference in C# not working?

Given the following C# code:

var a = new[] {"123", "321", 1}; //no best type found for implicitly typed array

And its counterpart in VB.NET:

Dim a = {"123", "321", 1} 'no errors

It appears that VB.NET is able to correctly infer type of a = Object(), while C# complains until the above is fixed to:

var a = new object[] {"123", "321", 1};

Is there a way to auto-infer type in C# for the above scenario?

EDIT: Interesting observation after playing with different types in a C# sandbox - type is correctly inferred if all elements have common parent in the inheritance tree, and that parent is not an Object, or if elements can be cast into a wider type (without loss of precision, for example Integer -> Double). So both of these would work:

var a = new[] {1, 1.0}; //will infer double[]
var a = new[] {new A(), new B()}; //will infer A[], if B inherits from A

I think this behavior is inconsistent in C#, because all types inherit from Object, so it's not a much different ancestor than any other type. This is probably a by-design, so no point to argue, but if you know the reason, would be interesting to know why.

like image 521
Neolisk Avatar asked Nov 20 '12 22:11

Neolisk


People also ask

Does C have type inference?

Type inference is a feature that is common to a variety of programming languages. While, in the past, it has been prominently present in functional ones (e.g., ML and Haskell), today, many object-oriented/ multi-paradigm languages such as C# and C++ offer, to a certain extent, such a feature.

What is the type of inference?

Type inference refers to the automatic detection of the type of an expression in a formal language. These include programming languages and mathematical type systems, but also natural languages in some branches of computer science and linguistics.

What is type inference in programming?

Type inference is the compile-time process of reconstructing missing type information in a program based on the usage of its variables. ML and Haskell are two languages where this aspect of compilation has enjoyed some popularity, allowing type information to be omitted while static type checking is still performed.

What is type inferencing as used in ML?

Standard ML is a strongly and statically typed programming language. However, unlike many other strongly typed languages, the types of literals, values, expressions and functions in a program will be calculated by the Standard ML system when the program is compiled. This calculation of types is called type inference.


1 Answers

No. Implicitly typed arrays in C# require that the type of one of the expressions in the array initializer is of the target type. Basically, the compiler tries to find exactly one element type such that all the other types can be converted to it.

You could cast any of the elements to object of course:

var a = new[] { (object) "123", "321", 1}; 

... but then you might as well just use an explicitly typed array initializer:

var a = new object[] {"123", "321", 1}; 

Or in cases where you really are declaring a variable at the same time:

object[] a = {"123", "321", 1};
like image 111
Jon Skeet Avatar answered Oct 09 '22 12:10

Jon Skeet