Yesterday I wrote an article explaning what var means in .NET 3.x. Today lets talk about Anonymous Types.
Anonymous Types as describe by the C# 3.0 specification are tuple types automatically inferred and created from object initializers. Anonymous Types allows the new operator to be used with an anonymous object initializer to create an object at compile time. The format for an anonymous type declaration is a follows
var v = new { p1 = e1 , p2 = e2 , ... px = ex }
where v is the var variable, px denotes the property name and ex is equaivalent value.
Enough with the theory and lets look at some actual code. Lets assume that I have a class called Person with the following properties:
public class Person
{
private string _name;
private int _age;
private bool _believesInJesus;
public string Name
{
get { return _name; }
set { _name = value; }
}
public int Age
{
get { return _age; }
set { _age = value; }
}
public bool BelievesInJesus
{
get { return _believesInJesus; }
set { _believesInJesus = value; }
}
}
Let's say that I want to create an Anonymous Type that has a similar structure from what we have above, all I need to do is make this call:
var human = new { Name = "Keith", Age = 25, BelievesInJesus = true };
What happens in compile time is that the .NET compiler generates a class that represents the structure of your Anonymous Type.
public class __Anonymous1
{
private string _name;
private int _age;
private bool _believesInJesus;
public string Name { get { return _name; } set { _name = value; } }
public int Age { get { return _age; } set { _age = value; } }
public bool BelievesInJesus { get { return _believesInJesus; } set { _believesInJesus = value; } }
}
It's an "Anonymous Type" because it a nameless class, it's generated by the compiler for you and it directly inherits from object.
To prove this let's look at this example:
class Program
{
static void Main(string[] args)
{
var person = new { Name = "Keith", Age = 25, BelievesInJesus = true };
Console.WriteLine(person.Name);
Console.WriteLine(person.GetType());
Console.ReadLine();
}
}
Hovering at the person variable would give us some information about it's type:

The code above will have this output:

As you can see, the compiler created an Anonymous Type Called <>f__AnonymousType0`3. One thing to note about Anonymous Types is that the compiler is smart enough to figure out if an Anonynous Type has already been declared that meets the schema requirement of your new Anonymous Type.
class Program
{
static void Main(string[] args)
{
var person1 = new { Name = "Keith", Age = 25, BelievesInJesus = true };
var person2 = new { Name = "Charissa", Age = 23, BelievesInJesus = true };
var person3 = new { Name = "Peter", Age = 23};
Console.WriteLine(person1.Name);
Console.WriteLine(person1.GetType());
Console.WriteLine(person2.Name);
Console.WriteLine(person2.GetType());
Console.WriteLine(person3.Name);
Console.WriteLine(person3.GetType());
Console.ReadLine();
}
}
The result would be:

I hope I was able to show you what Anonymous Types are in C#. Next up is Extension Methods.
Posted
01-23-2008 2:04 PM
by
keithrull