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?
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.
You can use:
var marketValueData = new object[] { new { A = "" }, new { A = "" }, new { B = "" }, ..., };
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