I've read some articles on how to create anonymous types in C#.
What are some use cases for these things? To me, it seems like it might make things a little more difficult to understand declaring objects and their members inline.
When does it make sense to use Anonymous Types?
I like to use anonymous types when I need to bind to a collection which doesn't exactly fit what I need. For example here's a sample from my app:
var payments = from terms in contract.PaymentSchedule
select new
{
Description = terms.Description,
Term = terms.Term,
Total = terms.CalculatePaymentAmount(_total),
Type=terms.GetType().Name
};
Here I then bind a datagrid to payments.ToList(). the thing here is I can aggregate multiple objects without havign to define an intermidary.
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