Explain Anonymous type in C#

Technology CommunityCategory: C#Explain Anonymous type in C#
VietMX Staff asked 3 years ago

Anonymous types allow us to create a new type without defining them. This is way to defining read only properties into a single object without having to define type explicitly. Here Type is generating by the compiler and it is accessible only for the current block of code. The type of properties is also inferred by the compiler.

Consider:

var anonymousData = new
{  
     ForeName = "Jignesh",  
     SurName = "Trivedi"
};  

Console.WriteLine("First Name : " + anonymousData.ForeName);