Anonymous types were introduced in C# 3.0, it allows you to declare type without specifying any explicit type information. Anonymous types are generally anonymous in nature, that means the type information are unknown to us.

C# allows us to declare anonymous types inside a method without explicitly specifying their type information. The compiler generates a name for the anonymous type and derives the property types on the fly.

If you haven’t gone through my previous article on implicitly typed local variable (Var), then you can do it here.

Anonymous Types usage & syntax :

We can declare an anonymous type by using implicitly typed local variable pattern and use Var keyword . It is mandatory to declare the variable type as var .

In below code, we define a class, having three properties ( FirstName , LastName of type System.String and Age of type System.Int32 ) and initializing their values.

If we look at the above code, we haven’t mention any explicit type after the new keyword. The compiler creates a anonymous type name automatically, which is unknown to us (that’s why its anonymous type).

The object initializer syntax is used here to declare and initialize the properties. Since we don’t know the type, hence the implicity typed local variable feature Var is used to have the compiler infer the type from the expression.

Anonymous Type_Output_1.0

So here compiler inferred the anonymous type and generated strongly typed properties . It created System.String type property for FirstName , LastName  and   System.Int32 type property for Age . The compiler looked at the assignment and based on it derived the property type.

What compiler does behind the screen when it sees  an Anonymous type ?

Lets dig deep and figure it out what compiler does when it finds an anonymous type.

For above code example, the compiler creates below line of IL code.

Anonymous Type Output_1.2

If you look at it closely then, here the compiler creates a class for the anonymous type. It also creates private read-only fields and public read-only property for each of the field (FirstName, LastName and Age). It also creates a constructor that accepts all these field values and initializes the private read-only fields inside it.

In addition to that, the compiler overrides System.Object type default methods like Equals , GetHashCode and ToString inside the class.

Below assembly dissembler code image can give a clear picture of the IL code.

Anonymous Type Output_1.3

Nested Anonymous Types :

If an anonymous type is present inside another anonymous type, then its called as nested anonymous type. Lets take below example.

If you debug and check the anonymous type in the quick watch then you will see something similar to below.

Output_1.4

As seen in the above image, the compiler creates Address an anonymous type inside student anonymous type and also creates the strongly typed properties based on their assignments.

Key Points :

  • If an assembly has two or more anonymous types that have similar structure (same properties and are in same order). In that case, the compiler will create a single definition of the anonymous type and create multiple instance of that type.
  • You can return an anonymous type from a method, however the return type should be an Object type and in the calling method you can use dynamic type as follows.

Output to above :

Output_1.5

  • The scope of an anonymous type declaration is always inside a method similar to that of Var type.
  • In an anonymous type, you cannot initialize a property to null. Following line will give compile time error.

Thanks for your time 🙂 feel free to provide your feedback and do check my other blogs on Dynamic type in C# , Nullable type in C# , Var type in C# and Reference Type and Value type .