What is anonymous type in C#?

 Posted by SheoNarayan on 12/22/2008 | Category: C# Interview questions | Views: 23848

This is a new feature in C# 3.0. This enable use to create a type/class on-the-fly at compile time.

For example

var emp = new { Name = "Sheo", Gender = "Male", Active = true };

In this case a new type will be created on the fly that will have Name, Gender, and Active as public property and its backing field like _Name, _Gender, _Active.

This is especially useful when we want to receive data from other object or iterate through a collection and set values and do not want to create a class just to hold the data. Note that anonymous types are just a placeholder, we can't customize its behavior or add methods inside it.

Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Login to post response