Anonymous Typed Classes in C#


Introduction

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 class that has no name and it can help us to increase the readability and maintainability of applications by keeping the declarations of variables much close to the code that uses it.

We create anonymous class by 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 };

Above class contains two public fields called Name as I have initialized a string "Abhimanyu" and Age as I have initialized an integer 42. Compiler generates its name and it is unknown to us.

Now think, if we don't know the name of the class then how we will create an object? This is called 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. 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};

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, 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 following (given below) then there is no any mistake because they have same order and same type. Sometimes it will not work as our expectations.

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.

Comments

Popular posts from this blog

Customize User's Profile in ASP.NET Identity System

Migrating database from ASP.NET Identity to ASP.NET Core Identity