Anonymous types in C#

These are like an object literals in JS (instead of : we use = in C#) and we use the new keyword in C# to define an anonymous type

  • Allows us to create an object that has read-only properties
  • No explicit type
  • To create anonymous types, we must use new operator with an object initializer
  • Can be nested too

var author = new
{
  FirstName = "Joydip",
  LastName = "Kanjilal",
  Address = "Hyderabad, INDIA"
};
Console.WriteLine(author.FirstName);

Comments