What are anonymous types in C#?

Anonymous types are data types in C# that hold public read-only attributes. Anonymous objects cannot contain functions, constructors, getter/setter methods, and so on, which sets them apart from regular classes.

Syntax

An anonymous type object can be declared using the following syntax in C#:

var obj = new {<attribute1> = <value1>,<attribute2> = <val2>...};
Anonymous type declaration

In C#, the built-in var keyword is used to declare an anonymous type. Since the attributes are not restricted to only one data type, this keyword is used to hold a reference to the anonymous type object. Furthermore, the new keyword is also required to declare an anonymous type object. It can be seen that the declaration is very similar to how we declare objects.

Inside the curly brackets, we need an attribute name followed by its value to store it inside the anonymous type object.

Examples

Let's declare an anonymous type object and see its workings.

Variables

Variables can be stored as attributes inside an anonymous type:

using System;
class Example
{
static void Main()
{
var obj = new {name = "Mary", age = 27, gender = 'F', salary = 20000.5 };
Console.WriteLine(obj);
Console.WriteLine(obj.name);
Console.WriteLine(obj.age);
Console.WriteLine(obj.gender);
Console.WriteLine(obj.salary);
}
}
  • Line 6: We declare an anonymous type object named obj that holds four attributes—name, age, gender, and salary. Each of these attributes has a different data type than the other, and that's why anonymous types are helpful.

  • Line 7–11: Printing each attribute inside the anonymous object. The dot (.) operator access an attribute inside the anonymous type object.

We discussed that the attributes of an anonymous type object are read-only. Let's test that out now:

using System;
class Example
{
static void Main()
{
var obj = new {name = "Mary", age = 27, gender = 'F', salary = 20000.5 };
obj.name = "Kevin";
obj.age = 30;
obj.gender = 'M';
obj.salary = 23000.12;
}
}

As seen in the above code, any reassignment to the attributes of an anonymous type object results in errors. So, we can only assign any value to the attributes at the time of declaration. Also, null values cannot be given to an attribute of an unknown data type.

Nested

We can have an anonymous object within an anonymous object like this:

using System;
class Example
{
static void Main()
{
var obj = new {
name = "Mary",
age = 27,
gender = 'F',
salary = 20000.5,
designation = new {company = "Educative", occupation = "Manager"}
};
Console.WriteLine(obj.name);
Console.WriteLine(obj.age);
Console.WriteLine(obj.gender);
Console.WriteLine(obj.salary);
Console.WriteLine(obj.designation);
}
}
  • Line 6–12: We declare an anonymous object with the attributes—name, age, gender, salary, and designation (an anonymous object itself). The new keyword is required before the nested anonymous object too.

  • Line 13–17: We print each attribute inside the anonymous object. It can be seen that the designation attribute is printed as an object itself.

Arrays

We can also create arrays of anonymous type objects:

using System;
class Example
{
static void Main()
{
var obj = new[] {
new {name = "Mary", age = 27, gender = 'F', salary = 20000.5 },
new {name = "Jake", age = 21, gender = 'M', salary = 10000.0},
new {name = "Lena", age = 19, gender = 'F', salary = 9300.25}
};
for(int i = 0; i < 3; i++){
Console.WriteLine(obj[i]);
}
}
}
  • Line 6: We create an array of anonymous objects by adding the square brackets ([]) after the new keyword.

  • Line 7–9: We declare individual anonymous objects and store them in the array. For each object, a new keyword is required.

  • Line 11–13: We use a loop to traverse and print each object in the array.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved