Anonymous Typed Classes in C#


MSDN Definition: Anonymous types provide a convenient way to encapsulate a set of read-only properties into a single object without having to explicitly define a type first. The name is generated by the compiler and is not available at the source code level or our application cannot access it. The type of each property is inferred by the compiler.

Anonymous types are a new feature introduced with C# 2.0. Anonymous class is a class that has no name and that can help us to increase the readability and maintainability of applications by keeping the declarations of variables closer to the code that uses it.

We create an object of the anonymous class using the new keyword and a pair of braces defining the fields and values that we want the class to contain. For example:

myAnonyClassObject = new { Name = "Abhimanyu", Age = 21 };

The above class contains two public fields called Name, initialized as the string "Abhimanyu", and Age, initialized as the integer 42. The compiler generates it's name and it is unknown to us.

Now think, if we don't know the name of the class then how will we create an object? This is called an anonymous class which can't be known.

After all we have one thing in hand; we can define it as variable by using var keyword. The var keyword causes the compiler to create a variable of the same type. For example:

var myAnonyClassObject = new {Name = "Abhimanyu", Age = 21};

Now, we can access anonymous class fields by using dot notation. For example:

Console.WriteLine("Name:{0} Age:{1}", myAnonyClassObject.Name, myAnonyClassObject.Age};

We can also create other instances of the same anonymous class but with different values. For example:

var newMyAnonyClassObject = new {Name = "Deepak", Age = 23};

The compiler uses the name, type, number and order of the fields to determine whether two instances of an anonymous class have the same type or not. In this case, the variables myAnonyClassObject and newMyAnonyClassObject have the same number of fields, same name, same type and same order so both variables are instances of the same anonymous class. If we write the following (given below) then there are not any mistakes because they have the same order and the same type. Sometimes it will not work as we expect.

newMyAnonyClassObject = myAnonyClassObject;

We can create an array of anonymous typed elements by combining an implicitly typed local variable and an implicitly typed array. For example:

var myAnonyClassObject = new[] {new {Name = "Abhimanyu", Age = 21}, new {Name = "Deepak", Age = 23}};

Restrictions of Anonymous Typed Classes

Anonymous classes can contain only public fields.

Anonymous class's fields must all be initialized.

Anonymous class members never are static.

Anonymous class cannot specify any methods.

Anonymous cannot implement interface.

Further Reference

Find a very-very good article written by Dhananjay on Anonymous Method.


Up Next
    Ebook Download
    View all
    View all