Back To Basics - Anonymous Types In C# 3.0

By Anoop Madhusudanan

Vote on HN


About Back To Basics - It's often good to look back and fill any gaps we might be having, in certain aspects of the language or framework we use. I'll be examining and blogging about some well known features of C# and .NET, in Back To Basics series - Probably in a bit more detail.




This post is about the Anonymous types in C#, and the object initializer syntax.

It is well known that starting from C# 3.0, you can initialize an object like this.
var boy = new { Name = "Jim", Age = 2 };
You specify the properties of an object, and at compile time, the compiler will create a new (anonymous) type, with the properties you specify, and with a constructor that can take the same number of arguments.

Now, let us examine our anonymous type in detail by using reflection. We'll examine whether the type is a class, along with the type's name. If you run this,
static void Main(string[] args)
        {

            var boy = new { Name = "Jim", Age = 2 };

            Console.WriteLine("Name=" + boy.GetType().Name);
            Console.WriteLine("BaseType=" + boy.GetType().BaseType.Name);
            Console.WriteLine("Asm=" + boy.GetType().Assembly.FullName);
            Console.WriteLine("IsClass=" + boy.GetType().IsClass);
        }

You'll get some output like
Name=<>f__AnonymousType0`2
BaseType=Object
Asm=AnonymousTest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
IsClass=True

Needless to say, the Name will be a random name, and the Assembly's name depends on your own project name. The key point is, we just established that the type is there in the assembly - which means, as I mentioned, the compiler created a type at compile time itself. You can have a look at the IL if you are interested.

Now, let us examine the observation that the anonymous type has a constructor that takes the same number of arguments as that of properties (i.e, in this case, the number of constructor arguments will be equal to the number of properties we specified in the object initializer, of types string and int). We can do that easily by creating a new instance of the anonymous type we have, using reflection. Add this to the tail end of the last piece of code.
//These two calls will fail with a run time exception, Just keep them commented
//var newboy = Activator.CreateInstance(boy.GetType(), null);
//var newboy = Activator.CreateInstance(boy.GetType(), new object[] { "Joe"});

//Only this will work. Creating a new boy
var newboy=Activator.CreateInstance(boy.GetType(),new object[]{"Joe",2});

//Just write back the name of new boy
Console.WriteLine(newboy.GetType().GetProperty("Name").GetValue(newobj, null));

You can see that only the last call will work, as we need to pass the same number of arguments while instantiating.

Note that you can have arrays of anonymous types as well, like this.
   var guys = new[]
            {
                    new {Name="Joe", Age=10},
                    new {Name="James", Age=22},
                    new {Name="Jim", Age=23},
                    new {Name="Jerard", Age=10},
            };

Further more, I just want to mention one more point. You may not need to assign the property explicitly, when you use the object initializer syntax. Consider this code.
            var guy1 = new { Name = "Ken", Age = 30 };
            var guy2 = new { guy1.Name, Age = 30 };

            Console.WriteLine(guy1.GetType().Name + " - " + guy1.Name);
            Console.WriteLine(guy2.GetType().Name + " - " + guy2.Name);


The key point to note here is, for guy 2, we are not explicitly specifying the Name property. Instead, we are just specifying that the name of Guy2 is same as Guy1. The compiler will assume the property name from the given parameter's name (in fact, the last part of the specified parameter, in this case 'Name') - This is called Projection. Obviously, you'll find that both guy1 and guy2 are of the same type as well.

Can you guess the output of this code as well?
            string Name = "Guy2";

            var guy1 = new { Name = "Ken", Age = 30 };
            var guy2 = new { Name, Age = 30 };

            Console.WriteLine(guy1.GetType().Name + " - " + guy1.Name);
            Console.WriteLine(guy2.GetType().Name + " - " + guy2.Name);


Why anonymous types are interesting? Because we use them in various ways, especially while using LINQ syntax. Here is a basic example

  var oldGuys = from guy in guys
                            where guy.Age > 20
                            select new { guy.Name };


Here, you'll probably find that we are using projection in the select new block. And as the last pointer, just debug and observe the type of oldGuys, and have a look at the base's type. I'll cover more on LINQ and Expression trees later.
Shout it
© 2012. All Rights Reserved. Amazedsaint.com