Pete On Software

RSS Feed

Anonymous Types in C# 3.0

For a long time, the var keyword has been the domain of only wholly dynamically typed languages. In fact, some people from the strongly-typed camp have considered the use of var as “hacky”, even though C#-ians have been able to treat everything as an object all along. Granted, it is not exactly the same and there is the whole boxing and unboxing thing to deal with, but I wanted to include full disclosure.

Now however, dynamic languages are all the rage and people are finding good uses for a generic variable type. To harness that power, one of the things done in C# 3.0 was the creation of Anonymous Types. Examine the code below and its output.

var blogPost = new

{

    Name = “Anonymous Types in C# 3.0”,

    Author = “Pete Shearer”,

    Category = “Code Tips”

};

 

Console.WriteLine(blogPost.Name);

Console.WriteLine(blogPost.Author);

Console.WriteLine(blogPost.Category);

Anonymous Types Output

By declaring my variable blogPost with the var keyword, I have created a dynamic variable. Now, after the new keyword and inside the curly braces, I declare a few properties and set their values. Behind the scenes, the compiler has declared a new object, given it a temporary name, and set the values in “traditional” fashion. It is even smart enough that if I declared another var with that definition, it wouldn’t redefine the anonymous type.

Within the anonymous type, the object remains strongly typed. Consider the following alteration to the code:

var blogPost = new

{

    Name = “Anonymous Types in C# 3.0”,

    Author = “Pete Shearer”,

    Category = “Code Tips”,

    Date = new DateTime(2008, 3, 27)

};

 

Console.WriteLine(blogPost.Date.GetType());

Anonymous Types Output 2

The compiler inferred the type from the object placed into it. I know that I said new DateTime there, but it would have worked for any type that I put into that property. This new feature certainly affords developers a new level of flexibility. Enjoy.

3 Comments

Brick  on March 31st, 2008

I’m leaving this comment to serve a couple purposes:

1. See! I actually do read your site 😛
2. To demonstrate my noobness
3. I have a legitimate question. What’s a scenario where you would use an anonymous type over a structure? They seem very similiar and structures are value types and so are placed on the stack (faster right?).

Pete  on April 1st, 2008

Brick!!!!

Great to hear from you. This link has a good use for anonymous types. Additionally, anonymous types are good for use in LINQ. This link talks a little about that (Edit: Link removed, as it is now a 404).

As for structs, if all you are doing is looking to store some data in the same way repeatedly, structs are still a legitimate way to go. Anonymous types shouldn’t battle too far into the struct space.

Anonymous types are strong for the things I outlined above, and also for use in testing or creating lightweight mocks. Let’s pretend you are waiting on another group to create an object for you and to populate it. But you have to test what you are doing. You could define the object somewhere in your code temporarily and then consume its properties, or you could define an anonymous type with the same variable name (one line of code), and then call it in your code exactly the same way that you will call the real objects properties when it is finished. I know that B-Mad used it in this way at his last job.

Brick  on April 1st, 2008

Ah yes, I see. Admittedly my knowledge of LINQ is nearly non-existent but I get the concept. This does make sense, though. Just when I thought I was making some progress on getting this C# stuff under my belt they go and create all this newfangled stuff in 3.0 and 3.5! Back in my day we had pointers, manual memory allocation, and raw speed baby. You were lucky to get OOP. Seriously though thanks for the response 😛 If you’ll excuse me I’ve got to go pay for groceries with nickels, chase kids off my lawn, and complain about my pension.

Leave a Comment