Generic methods with anonymous types
suggest changeGeneric methods allow the use of anonymous types through type inference.
void Log<T>(T obj) {
// ...
}
Log(new { Value = 10 });
This means LINQ expressions can be used with anonymous types:
var products = new[] {
new { Amount = 10, Id = 0 },
new { Amount = 20, Id = 1 },
new { Amount = 15, Id = 2 }
};
var idsByAmount = products.OrderBy(x => x.Amount).Select(x => x.Id);
// idsByAmount: 0, 2, 1
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents