Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No best type found for implicitly-typed array

Tags:

c#

Can someone explain me why this code:

var marketValueData = new[] {     new { A = "" },     new { A = "" },     new { B = "" }, }; 

Is giving me the error:

No best type found for implicitly-typed array

while this one works perfectly fine:

var marketValueData = new[] {     new { A = "" },     new { A = "" },     new { A = "" }, }; 

Apart from a different property (B in the last entry of the first example), they are the same. Yet the first one is not compiling. Why?

like image 322
Saturnix Avatar asked Oct 23 '13 07:10

Saturnix


2 Answers

It's because you have two different anonymous types in the first example, the definition of the last item is different than the other ones.

In the first example , one containing an A property and one containing a B property, and the compiler can't figure out the type of array. In the second example there is one anonymous type, containing only A.

I think it's a typo, so you can change B to A in last entry in first example

From MSDN:

You can create an implicitly-typed array in which the type of the array instance is inferred from the elements specified in the array initializer.

like image 136
Kamil Budziewski Avatar answered Sep 20 '22 23:09

Kamil Budziewski


You can use:

var marketValueData = new object[] {     new { A = "" },     new { A = "" },     new { B = "" },     ..., }; 
like image 22
Yas Avatar answered Sep 21 '22 23:09

Yas