Overview of C# Anonymous Types

You are currently viewing Overview of C# Anonymous Types

Microsoft introduced some great new features in C# 3.0 to make developers more productive. Most of those features are introduced to support Language Integrated Query (LINQ) but they can also be used in many other scenarios. One such feature is Anonymous Types that allows you to define a class with some simple encapsulated fields without any associated methods, events or functionality. In this tutorial I will give you overview of C# 3.0 Anonymous Types.

What is Anonymous Type?

There are times in programming when you would like to define a class that holds data temporarily and which is not required to be reused anywhere else in the application. This is where anonymous type helps which provides you massive shortcut for creating types on the fly by using extended syntax of the new operator. When you define anonymous type you also use object initialization syntax along with the new var keyword.

For the purpose of this tutorial, I have created a simple C# Console Application in Visual Studio 2008. Inside the main Program I am creating the following anonymous type to illustrate its syntax.

var studentInfo = new { ID = 1, Name = "Waqas", Fee = 3000.00 };

Or

var studentInfo = new { 
   ID = 1, 
   Name = "Waqas", 
   Fee = 3000.00 
};

The above anonymous type is representing a Student object that has few properties such as IDName and Fee. The first thing you should note that anonymous types are types with no name and as a programmer you don’t have any control on the name of anonymous type. That’s why you need to implicitly typed it by using var keyword. At compile time, the C# compiler will generate a uniquely named class automatically and because you don’t have information about the name of the type so the use of var keyword is mandatory.

The second thing to notice in the above syntax is the use of new keyword without the type name and the use of object initializer syntax to define and set the type properties and their values. When you create anonymous type the compiler automatically build a type inherited from System.Object and then called its default constructor behind the scene. Once you have defined the properties of your anonymous type you can access them by using normal C# syntax as shown below:

Console.WriteLine("Student ID: " + studentInfo.ID); 
Console.WriteLine("Student Name: " + studentInfo.Name); 
Console.WriteLine("Student Fee: " + studentInfo.Fee);

Keep in mind here is that all the properties you define inside your Anonymous Type are read only so following line will never compiler.

studentInfo.ID = 4;

As I mentioned that all Anonymous Types inherit from System.Object by default so they have access to the methods such as ToString()GetType() etc. In the case of anonymous type the ToString() method returns a string that has all the properties and their values inside. For example if you will call the ToString() method of our studentInfo object you will get the output shown below. Notice how curly braces are attached automatically with name/value pairs.

{ ID = 1, Name = Waqas, Fee = 3000 }

It is also possible to compose one anonymous type inside another anonymous type. Check the following example:

var emp = new { 
   ID = 1, 
   Name = "Ali", 
   Address = new { 
      Street = "Baker Street", 
      City = "London", 
      Country = "UK" 
   } 
};

In the example above I have created an anonymous type holding employee ID and Name and another anonymous type to store the employee Address information. You can access both anonymous types as you access normal classes and their composed objects.

Console.WriteLine(emp.ID); 
Console.WriteLine(emp.Name); 

Console.WriteLine(emp.Address.Street); 
Console.WriteLine(emp.Address.City); 
Console.WriteLine(emp.Address.Country);

Although anonymous types provides you very quick and short syntax to create types in your applications but you would never stop defining strongly typed classes because of the following limitations.

  1. You don’t have control on the name of the anonymous type
  2. Anonymous Type always inherit from System.Object
  3. The properties of an anonymous type are read only
  4. You cannot create events, operators or methods inside anonymous type
  5. You cannot override any method of System.Object in anonymous type
  6. Anonymous types are sealed types so you cannot inherit from anonymous type
  7. Anonymous types are always created using default constructor.
READ ALSO:  Introducing Windows Communication Foundation (WCF)

With all the above limitations in mind, you may be thinking why you would ever need to use anonymous types. The answer is that you need to use them when you will start using LINQ queries because you will find the anonymous type syntax very quick and useful in many cases.

This Post Has One Comment

  1. Haroon Ali

    Sir, Its a good article.

Leave a Reply