Anonymous type in C#

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

Comments

Popular posts from this blog

Enable HTTP Transport Security (HSTS) in IIS 7

When to use Truncate and Delete Command in SQL

Steps To Create New Project/Team On TFS