Bill McCarthy explained this to me in an email.
He wrote about it in this excellent article in Visual Studio Magazine "Drill Down on Anonymous Types".
But it still bit me in the ass!
So I am stamping this into my memory:
| in a query |
results in |
| ...select cust.CustomerID.cust.CompanyName... |
Immutable anonymous type |
...select New With { .CustomerID=cust.CustomerID, .CompanyName=cust.CompanyName} ... |
Mutable anonymous type |
...select New With { Key .CustomerID=cust.CustomerID, Key.CompanyName=cust.CompanyName} ... |
Immutable anonymous type |
| Direct creation of object |
|
New With { .CustomerID=cust.CustomerID, .CompanyName=cust.CompanyName} |
Mutable anonymous type |
New With { Key .CustomerID=cust.CustomerID, Key .CompanyName=cust.CompanyName} |
Immutable anonymous type |
And just in case someone who really needs this stumbles on this post, two clarifications:
- All anonymous types are immutable in C#.
- Mutable means changeable, editable. Immutable means written in stone.