Demystifying C# 3.0 - Part 2: Anonymous Types

Posted on 6/30/2006 @ 8:41 PM in #Vanilla .NET by | Feedback | 12365 views

In my last blog entry, I had talked about Implicitly typed local variables and the "var" keyword. If you have stumbled across this post through a search engine, or otherwise, I would recommend reading that first.

Assuming that you understand "var" - lets dive into part 2 of Demystifying C#3.0 ~ Anonymous Types.

In C# 2.0, lets say you wanted to represent a person, you would typically have to write a class as follows -

public class Person
{
   string hairColor ;
   string skinColor ;
   int teethCount ;
} // I'm obviously taking the shortcut by not creating properties .. anyway, that's besides the point.


Well, in LINQ, I could be querying arbitrary groups of my data. When I'm querying "Customers", and the above is a subset of "a customer" - what a royal pain in the rear it would be if I had to first write the structure of my class, before I can use the class? (Duh!). While that sounds like "the thing to do" - you immediately lose the ability to query any arbitrary structures of data at runtime.

Enter anonymous types. Now you can represent arbitrary clumps of data, without having to declare their structure first. Here is how.

var monster = new {hair="black", skin="green", teethCount=64} ;

The above line of code will work even if  you do not declare a class called "Person" in advance. This lets you arbitrarily create newer structures to represent your data, without having to define them at "coding time" first.

The above line of code simply generates a class under the scenes (you never see it), that looks like as below -

class __Anonymous1
{
   private string _hair = "black";
   private string _skin = "green";
   private int _teeth   = 64;

   public string hair {get { return _hair; } set { _hair = value; }}
   public string skin {get { return _skin; } set { _skin = value; }}
   public int teeth {get { return _teeth; } set { _teeth = value; }}
}


Thus this class is an "Anonymous Type" - it has no name, it was generated for you by the C# compiler, so you can represent and hold any arbitrary clump of data, but yes it is a "System.Type".

The rationale behind the creation of this was to be able to query for any arbitrary set of data, without having to declare it's structure first. That would be a bit essential for LINQ I'd say ;-).

 

Sound off but keep it civil:

Older comments..


On 8/4/2006 1:34:56 PM Jason Bunting said ..
Hmm...

C# 3.0:

var monster = new {hair="black", skin="green", teethCount=64};

JavaScript:

var monster = {"hair":"black", "skin":"green", "teethCount":64};

Looks like JSON almost.


On 8/4/2006 5:50:20 PM Sahil Malik said ..
Jason, but var in Javascript and var in C# are very very different.


On 8/5/2006 10:49:58 PM Jason Bunting said ..
Sure - I get the difference, pretty easy to understand thanks to your brief description. I was more or less commenting on the syntax of declaring an anonymous type, and the similarity to declaring an anonymous type in JavaScript, that's all. :)


On 8/6/2006 7:17:59 PM Sahil Malik said ..
Thanks Jason. A lot of folks write off Anonymous types as Variant .. which is why I thought I'd make it absolutely clear to anyone who comes and reads this blogpost after you and I are done discussing it :).


On 1/10/2007 2:01:14 PM mark said ..
great explanation but are sure that teethCount=64 should not be teeth=64 in monster's declaration?


On 1/10/2007 2:25:18 PM Sahil Malik said ..
Thanks Mark - I'll fix that later sometime. Thanks for pointing it out.


On 3/27/2007 9:46:11 AM Jim Anderson said ..
Naming the new keyword "var" probably didn't help with the confusion concerning comparisons to variants. Do you know what this is supposed to stand for - "variable"?


On 6/26/2007 11:20:26 AM Stephen Viswaraj said ..
Hi, itz a really good to understand about Anonymous Types.

I have a Question:


var monster = new {hair="black", skin="green", teethCount=64} ;

If I have my own Class which has the properties same as above mentioned(hair, skin,...)


Will Compiler use that Class instead of creating it.

Thanks,


Stephen Viswaraj.