anonymous types is the new feature in c# 3.0 . Which allows to create a class on the fly without any name. This is very useful while fetching value from LINQ query
- Anonymous types are new feature being added in C# 3.0.
- These are data types being generated on the fly at the run time.
- These data types are generated through complier rather than explicit class definition.
Program.cs
using System;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
namespace ConsoleApplication17
{
classProgram
{
staticvoid Main(string[] args)
{
varMyAnonClass = new
{
Name = "Scott",
Subject ="ASP.Net"
};
Console.WriteLine(MyAnonClass.Name);
Console.Read();
}
}
}
Output

A class without name is being generated and assigned to implicitly type local variable
How it works internally?
1. For an anonymous type syntax compiler generates a class.
2. Properties of the complier generated class are value and data type given in declaration.
3. Intellisense supports anonymous type class properties.

4. Properties of one anonymous type can be used in declaration of another anonymous type.
varSecondAnoynClass = new
{
info = MyAnonClass.Name + MyAnonClass.Subject
};
Program.cs
using System;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
namespace ConsoleApplication17
{
classProgram
{
staticvoid Main(string[] args)
{
varMyAnonClass = new
{
Name = "Scott",
Subject = "ASP.Net"
};
Console.WriteLine(MyAnonClass.Name);
Console.Read();
varSecondAnoynClass = new
{
info = MyAnonClass.Name + MyAnonClass.Subject
};
Console.WriteLine(SecondAnoynClass.info);
Console.Read();
}
}
}
Output
