Anonymous Types

Well, Connecticut had a small ice storm last night, and I’m one of the lucky ones who still has power.  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: ,,,,,

Related Post

One Response to “Anonymous Types”

  • hi,
    Thank you for the best explanation of anonymous types i’ve found so far on the web. there are many good programmers but not so many good teachers.

    best regards,
    natalie reznik

Leave a Reply

Comment Policy:

  • You must verify your comment by responding to the automated email that is sent to your email address. Unverified comments will never show.Leave a good comment that adds to the conversation and I'll leave your link in.
  • Leave me pure spam and I'll delete it.
  • Leave a general comment and I'll remove the link but keep the comment.

Notify me of followup comments via e-mail