Anonymous Types

You can also get the feed via email. Sign up now by supplying your email address and clicking the Subscribe button

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 = NewWith {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:

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.

Readable CodeTime out today for just a bit of a rant.Several years 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 […]

Multi-TargetingOne 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 […]

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 […]

About Dave Bush

Dave Bush is a Full Stack ASP.NET developer. His commitment to quality through test driven development, vast knowledge of C#, HTML, CSS and JavaScript as well as his ability to mentor younger programmers and his passion for Agile/Scrum as defined by the Agile Manifesto and the Scrum Alliance will certainly be an asset to your organization.