Search This Blog


C# 3.0 Tutorial -5:Anonymous Types

Anonymous Types

Anonymous simply means "without a name", and you can safely read the word "type" as "class" in this case. That is, in this section we are going to discuss the idea of classes without names.

In C# 2.0 we saw the introduction of anonymous methods. One of the consequences of a method having no name is that we had to take a reference to it - stored in a delegate type - right away, so we had some way to refer to it. The analogy with anonymous classes is that we are required to instantiate them right away. Therefore, the construct for creating an anonymous class also instantiates that class.

In C# 3.0, anonymous classes are greatly limited compared to standard classes. They can only inherit from object and their only memebers are private fields each with a matching read/write property.

With all of these things in mind, let's see how we declare and instantiate an anonymous type.
var MyProduct = new {
Name = "Vacuum Cleaner",
Price = 94.99,
Description = "Really sucks! Have your carpets clean in no time."
There are a couple of things to notice here. First is that we do not have a name for the class. Therefore, there is no type that we can write before the name of the variable when declaring it. What we can do, however, is to write "var", which leaves the compiler to work out the type for us. While the types are anonymous as far as we should care, the compiler and runtime actually do have some way of identifying them.

The second thing to notice is that we have used the "new" keyword but without specifying a type name. Instead, we have placed something after it that looks just like the object initializers we were looking at a few moments ago. This is not a co-incidence: we actually are initializing the object created by new. The question is, where is the definition of the class?

The class is created by looking at the initializer. For each name assigned to inside the initializer (Name, Price and Description in this case), a private field is created along with a get/set property. In this case, the class might look like this:
class __NO_NAME__ {
private string _Name;
private double _Price;
private string _Description;

public string Name {
get { return _Name; }
set { _Name = value; }
public double Price {
get { return _Price; }
set { _Price = value; }
public string Description {
get { return _Description; }
set { _Description = value; }
Note that the types of the fields are worked out by looking at what is being assigned to the property. Therefore, you are not allowed to assign a null value. It is the same type inference process that we have seen time and time again in C# 3.0.

Since anonymous classes are just classes and instances of them are just objects, you can do all of the things you'd expect to be able to with them, from simple things like accessing their properties through to more complicated things such as reflection.

Code Snippet(Aspx page)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class CSharp3_AnonymousType : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)

//Anonymous Types
//Its look likes a class which have three following property
//it is read only property ,we can not change the value further
var MyProduct = new { CustName = "Manab", CustID = 100450, Location = "Kolkata" };
Response.Write("Cust Name : " + MyProduct.CustName + " Cust ID:" + MyProduct.CustID);


Cust Name : Manab Cust ID:100450

tags:what is Anonymous Types in c# 3.0/3.5,new features in c# 3.0/3.5,how can we implement anonymous types in c# 3.0/3.5

No comments: