C# 3.0 Features. Part IV: Anonymous Types
Previous materials of C# 3.0 Feature series:
"Dynamic" Classes
Anonymous Type is basically the C# way how we can dynamically create, populate and use a class without type declaration. If for some reason we need temporary classes as data carrier without any business logic implementation then Anonymous Types could be the perfect solution. Instead of having statically declared bunch of standard classes we can eliminate our efforts and use Anonymous types. Actually this feature mainly was created to support Expression Tree and LINQ which would be hard to implement without it.
Anonymous type declaration syntax is very simple.
1: var someObject = new {Name = "Bob", Age = 23};
As you can see we used three technics introduced by C# 3.0 features: Auto Property, new Object initialization syntax and var keyword. Without any of them it won’t be possible to have Anonymous Types in game.
How does it work?
Actually Anonymous Type is not such “anonymous” like it sounds. What actually happens is that during the compilation compiler creates “static” type for us.
If we compile the application and look at the compiled IL code then we will find some strange f_AnonymousType0’2 class.

This is our Anonymous Type introduced by compiler which actually under the hood creates type with public properties.

Usage
After declaration of object based on Anonymous Type we are free to use it in a standard way as we do with standard classes including intellisense support.

1: var someObject = new {Name = "Bob", Age = 23}; 2: Console.WriteLine("Name is {0}, age is {1}", someObject.Name, someObject.Age);