Reputation: 3708
When you try to compile this:
var car = new { "toyota", 5000 };
You will get the compiler error "Invalid anonymous type member declarator. Anonymous type members must be declared with a member assignment, simple name or member access." because the compiler is not able to infer the name of the properties from the respective expressions. That makes total sense.
What makes me curious is that the error message implies three valid ways to declare a type member. Member assignment and member access are obvious:
// member assignment
var v = new { Amount = 108, Message = "Hello" };
// member access
var productQuery =
from prod in products
select new { prod.Color, prod.Price };
What would be an example of declaring with a simple name?
Googling and the related questions on SO result in examples of member assignment and member access only.
Upvotes: 14
Views: 11596
Reputation: 34832
As far as I know, simple name
declaration is this:
var amount = 10;
var whatever = "hello";
var newType = { amount, whatever }
Which will automatically create an anonymous type equal to:
var newType = { amount = amount, whatever = whatever }
Upvotes: 14