Blogs

Paul Kimmel's Blog

Did You Know?

     

I read and re-read help documentation, white papers, blogs, and tweak and poke around code to find out what is hiding in the crevices. Today I found something that I don’t think I have seen before. You probably know of course that you can define anonymous types with initializers in VB, as shown here:

Dim company = New With {.CustomerID = "PAULK", .CompanyName = "Kimmel's Konfections"}

The preceding code will cause the compiler to generate a class and the names in the initializer list—CustomerID and CompanyName—will be added as properties to the class. The type of these properties will inferred by the values assigned to them.

Did you know that you can nest anonymous type initializers? By using the same notation in the initializer list you can specify nested anonymous types. The following code defines an anonymous class containing a CustomerID and CompanyName and a nested type containing a phone number.

Dim company = New With {.CustomerID = "PAULK", _
    .CompanyName = "Kimmel's Konfections", _
      .Phone = New With {.Number = "(517) 555-1212"}}

If you write the class to the console you can see the contents as they are logically laid out (see Figure 1). If you look at the anonymous type in Reflector then you can see that the very short statement generates a complete class with, in this case, a nested class (see Figure 2).

image 
Figure 1: The layout of the anonymous type shows that Phone is a nested type.

image
Figure 2: The anonymous type with the nested type assigned to Phone.

 

Published Oct 27 2009, 06:44 PM by Paul Kimmel (DevExpress)
Bookmark and Share

Comments

 

Danny van Kampen said:

Nice !!

October 27, 2009 4:40 PM
 

Marco said:

I'm not a VB developer, but in C# there are also these 'anonymous' things.

And it's not to hurt you, but what to your opionion is the real benefit of being able to create such an anonymous thing?

To my opinion, it's difficult to understand what you're creating, what its semantics are and so my question: Nice that the compiler is able to infer a correct typing for the Company and the PhoneNumber but what's the use?

October 27, 2009 6:18 PM
 

Paul Kimmel (DevExpress) said:

Marco:

Befiore anonymous types we had to manually define every single class. Lookups, bits of classes, composition, for query results and everythng else. Sometimes you only need a type in an isolated, single instance. With anonymous types you are essentially letting the compiler code gen the class for you. Anonymous types are very good indeed. Could you imagine that for every LINQ query for instance you had to per-determine the shape of the result type and pre-define the class? That would be an awful lot of overhead.

October 27, 2009 8:08 PM
 

Steve Sharkey said:

Within a tight scope of linq queries I can see the point but otherwise it all sounds like lazy programming and what we used to think of as bad practice. Still computing is cyclical - I've lost count of the number of mainframe/smart client type models we have switched between now.

October 28, 2009 3:20 AM
More from DevExpress
Live Chat
Have a pre-sales question?
Need assistance with your evaluation?
We are here to help.
Chat is one of the many ways you can contact members of the DevExpress Team. We are available Monday-Friday between 8:30am and 5:00pm Pacific Time.
If you need additional product information, require pre-sales assistance, or want help with your order, write to us at info@devexpress.com or call us at
+1 (818) 844-3383.