Anonymous types in C# allows you to declare a type without specifying any explicit type information, which means the type information are unknown to us until the runtime.
This feature introduced in C# 3.0 , it 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 during runtime.
If you haven’t gone through my previous article on implicitly typed local variable (Var), then you can do it here.
How to declare an Anonymous types in C# :
We can declare an anonymous type by using implicitly typed local variable pattern using var keyword .
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.
1 |
var student = new { FirstName = "Johny", LastName = "Johny", Age = 18 }; |
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.
So, here the C# 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 C# compiler does when it sees a Anonymous types in C# ?
Lets look in to more detail and figure out what the C# compiler does when it finds an anonymous type.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
using System; namespace AnonymousTypeDemo { class Program { static void Main(string[] args) { var student = new { FirstName = "Johny", LastName = "Johny", Age = 18 }; } } } |
For above code example, the compiler creates below line of IL code.
If you look at it closely then, here the compiler creates
- A class for the anonymous type.
- It 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.
Nested Anonymous types in C# :
If an anonymous type is present inside another anonymous type, then its called as nested anonymous type. Lets take below example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
using System; namespace AnonymousTypeDemo { class Program { static void Main(string[] args) { var student = new { Id = 10001, Name = "Jane G", Age = 25, Gender = "Female", Address = new { Street = "77, Geary Street", City = "San Francisco", State = "CA", Zip = "94108", Country = "US" } }; } } } |
If you debug and check the anonymous type in the quick watch then you will see something similar to below.
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
namespace AnonymousTypeDemo { class Program { static void Main(string[] args) { dynamic name = GetAnonymousType(); Console.WriteLine("Name : {0}", name.Name); Console.WriteLine("Age : {0}", name.Age); } public static object GetAnonymousType() { return new { Name = "Ali", Age = 10 }; } } } |
Output to above :
- 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.
1 |
var stu = new { Name = "Ali", Age = null }; //Age = null will throw compile 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 .