Anonymous Types

So, let’s get to work looking at anonymous types in .NET.

As you know, last week we looked at object initialization.  Object initialization allows us to instantiate an object and assign properties all in “one line of code.”  Anonymous types takes this to the next step by allowing us to just completely skip the step where we declare the class.  Of course, what we end up with is a “Class” that has nothing but properties in it.  But, this can be quite useful in some cases.

For example, you might need to create a class that has a display field and a value field so that you can create a list that you can bind to a dropdown or regular list box.  In the past, we would have had to create a class that essentially had those two properties in it.  With anonymous types, we don’t have to.

So, in CSharp, all we have to do to create a new class that has nothing but properties in it is to write:

        var c = new 
            FirstName = "Dave", 
            LastName = "Bush" 


and in VB, it would look like:

        Dim c = New With {Key .FirstName = "Dave", .LastName = "Bush"}

You’ll notice that in both cases, the syntax is very similar to the syntax that we used for object initialization, but in this case, we’ve left out the class name (and the class declaration) and the compiler has supplied that for us.

One of the ways I mentioned that you might use this is so that you can databind the object to a control.  The CSharp code for this would be:

        var l = new List<object> 
            new {FirstName = "Dave",LastName="Bush"}, 
            new {FirstName = "Henry",LastName="Schmit"} 


Note that I had to declare the List as a List of objects because I don’t know what the type of the object will be.  But, for simple data binding purposes, this would be fine.

To do the same type of thing in VB, your code would look like:

        Dim l As Object() = _ 
            {New With _ 
              {.FirstName = "Dave", .LastName = "Bush"}, _ 
              New With {.FirstName = "Henry", .LastName = "Schmit"} _ 


Note that in VB, we need to use an object array, which could then be past on to a List object, but there does not appear to be any way to initialize a List object inline like we can in CSharp.  If you know how to initialize a List in VB.NET, leave me a comment.

Finally, you might think that creating a lot of anonymous types that all look the same would create a lot of extra IL code.  But, the compiler is smart enough to merge all of the similar looking anonymous types together. Tags: ,,,,,

Technorati Tags: ,,,,,

Like this Article? Subscribe to get every article sent to your email.

Related Post

  • Object Initialization in CSharp 3.0 and VB.NET 9Object Initialization in CSharp 3.0 and VB.NET 9 Yesterday we looked at the new var keyword in CSharp.  This makes CSharp variable declaration similar to VB.  After all, they've had the DIM keyword for years which essentially does the […]
  • Intellisense Everywhere in VB 9 (and a small bug)Intellisense Everywhere in VB 9 (and a small bug) One of the features the CSharp guys have had for quite a while is Intellisense everywhere.  Want to create a private variable?  Just start typing private and hit the tab key as soon as the […]
  • Readable CodeReadable Code Time out today for just a bit of a rant.A couple of days ago, I posted a top ten list for why you shouldn't buy Visual Studio 2008.  Most people totally got that this was all tongue in […]
  • Lambda Expressions. Other than LINQ…Lambda Expressions. Other than LINQ… ... what are they good for?If you haven't caught on yet, I'm inching my way toward LINQ. By the time I get to it, it will probably be about a 5 - 10 minute presentation just […]
  • Multi-TargetingMulti-Targeting One of the big new features that have been advertised with Visual Studio 2008 is this thing called Multi-Targeting.  This lets you write code for .NET 2.0 (or 3.0) even though 2008 is […]

About Dave Bush

Dave Bush is a .NET programmer and Certified ScrumMaster who is passionate about managing risk as it relates to developing software. When he is not writing or speaking about topics related to Application Lifecycle Risk Management (ALRM), he is an example to his peers as he develops web sites in the ASP.NET environment using industry best practices.

One Pingback/Trackback