Wednesday, January 23, 2008

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:

  1. All anonymous types are immutable in C#.
  2. Mutable means changeable, editable. Immutable means written in stone.
VB
Wednesday, January 23, 2008 4:48:31 PM (Eastern Standard Time, UTC-05:00)  #     |  Comments [3]  |