Definition
Anonymous type, as the name suggests is a type that doesn't have any name.Anonymous types are the new concept in C#3.0 that allow us to create new type without defining them. This is a way to define read only properties into a single object without having to define type explicitly. The type name is generated by the compiler and is not available at the source code level. We can create an object with the new keyword without defining its class.
Example
- var Anonymous_Type = new
- {
- firstName = "Pankaj",
- middleName = "Kumar",
- lastName = "Choudhary",
- age = 21,
- City = "Alwar"
- };


Above Anonymous type contain 5 fields. We can see that we don’t require to maintain the data type for any field on anonymous object. We can see that City is a string type and age is integer type property. So compiler applies the appropriate type to each property based on the value expression.
An anonymous type is a temporary data type that is inferred based on the data that we include in an object initializer. Properties of anonymous types are read-only in nature so cannot change their values. Let us try to change the value of Age.

Above error confirm that we can’t change property of any Anonymous type object.
Nested Anonymous Type
- var Anonymous_Type = new
- {
- Name = new
- {
- firstName = "Pankaj",
- middleName = "Kumar",
- lastName = "Choudhary"
- },
- age = 21,
- City = "Alwar"
- };
We can define an anonymous type that contain another anonymous type as a property. In above example we defined an anonymous type that is “Anonymous_Type” and this anonymous type contain a nested anonymous type “Name”.

Scope of Anonymous Type
Anonymous types are locally scoped. We can’t use a anonymous typed variable as a parameter for a method. But we can pass anonymous type for a method that accept dynamic type.
Example
- class Program
- {
- static void Main(string[] args)
- {
- var Anonymous_Type = new {
- firstName = "Pankaj",
- middleName = " Kumar",
- lastName = " Choudhary",
- age = 21,
- City = "Alwar"
- };
- Call(Anonymous_Type);
- Console.ReadKey();
- }
- static void Call(dynamic Data)
- {
- Console.WriteLine("My Name is: " + Data.firstName + Data.middleName + Data.lastName);
- Console.WriteLine("My Age is: " + Data.age);
- Console.WriteLine("I live in: " + Data.City);
- }
- }



Ashish SrivastavaPosted Feb 4, 2016, 5:06 AM
Welcome Pankaj Kumar Choudhary
Pankaj Kumar ChoudharyPosted Jan 14, 2016, 8:44 AM
Thanks Ehsan Sajjad.........
Pankaj Kumar ChoudharyPosted Jan 14, 2016, 8:43 AM
Thanks Ashish Srivastava ........
Pankaj Kumar ChoudharyPosted Jan 14, 2016, 8:43 AM
Thanks Anu Vivin Sir...........
Pankaj Kumar ChoudharyPosted Jan 14, 2016, 8:43 AM
Thanks Ankit Bansal Sir........
Pankaj Kumar ChoudharyPosted Jan 14, 2016, 8:42 AM
Thanks Debasis Saha Sir...........
Pankaj Kumar ChoudharyPosted Jan 14, 2016, 8:42 AM
Thanks Raja T
Pankaj Kumar ChoudharyPosted Jan 14, 2016, 8:42 AM
Thanks Humayun Kabir Mamun Sir.......
Ehsan SajjadPosted Jan 14, 2016, 7:37 AM
Good one
Ashish SrivastavaPosted Jan 14, 2016, 5:41 AM
good
Anu VPosted Jan 14, 2016, 2:04 AM
good..
Ankit BansalPosted Jan 14, 2016, 12:14 AM
thanks for sharing..keep it up
Debasis SahaPosted Jan 13, 2016, 11:49 PM
Nice Article....
Raja TPosted Jan 13, 2016, 11:31 PM
Nice, Thanks for sharing
Humayun Kabir MamunPosted Jan 13, 2016, 10:38 PM
Nice...