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]  | 
Wednesday, January 23, 2008 9:15:57 PM (Eastern Standard Time, UTC-05:00)
An extension of what you are saying...

All properties of the anonymous type that are marked with "Key" are immutable. Other properties are no immutable.

As you say, C# uses entirely immutable types. VB gives you the option of fully immutable types, fully mutable types and anonymous types in which some things can be changed and others are immutable. Flexibiity is good, if occasionally confusing.
Kathleen Dollard
Wednesday, January 23, 2008 9:33:33 PM (Eastern Standard Time, UTC-05:00)
I didn't want to say it quite that way only because then I have to further explain how the shortcut method in the first example is interpreted by VB as a shortcut for all Key'd properties. It's a mouthful and Bill explains it so nicely.

I look forward to more from you on this one, Kathleen! :-)
Julia Lerman
Thursday, January 24, 2008 12:02:53 AM (Eastern Standard Time, UTC-05:00)
Thanks ! :)
All comments require the approval of the site owner before being displayed.
Name
E-mail
Home page

Comment (HTML not allowed)  

Enter the code shown (prevents robots):

Live Comment Preview