In c#, anonymous types are useful for creating an object containing a set of read-only properties without specifying its type using the new keyword. The name and type for the properties in an anonymous type object will automatically be generated by the compiler.
Generally, the reference of anonymous types can be held by var type variables. So, we need to declare the variable by using the var keyword to assign anonymous objects.
Following is the example of creating an anonymous type object with a set of properties in c#.
If you observe the above code, we created an anonymous type object with three properties using new
keyword and assigned the object to the variable (userInfo), which is defined with the var keyword.
Here, we need to remember that the expression we used to assign a value to the property in the anonymous type object cannot be null.
As we discussed, the compiler will automatically assign the name and type for the properties in an anonymous type object. If you check the following image, when we try to access the properties of an anonymous object, the compiler automatically generated a new name for the anonymous type and applied the appropriate type for the properties based on the value expressions.
The compiler has assigned int
type for Id, string
type for Name and bool
type for IsActive property based on the values which we assigned to respective properties.
Following is the example of defining and accessing the properties of an anonymous object in c#.
When we execute the above code, we will get the result as shown below.
In c#, the access scope of the anonymous type is limited to the method where it has been defined. If we want to send the anonymous type to another method, that method must accept the parameter of dynamic type, but that is not recommended.
Following is the example of declaring and sending anonymous type as a parameter to another method using dynamic type in c#.
If you observe the above example, we send an anonymous type object (userInfo) as a parameter to the method (GetDetails) that accepts a parameter of the dynamic type.
When you execute the above example, you will get the result as shown below.
In c#, the nested anonymous types can achieve by defining the anonymous type within another anonymous type as a property.
Following is the example of defining the nested anonymous types in c#.
If you observe the above example, we defined an anonymous type object within another anonymous type object as a parameter to achieve nested anonymous type functionality.
When you execute the above example, you will get the result as shown below.
This is how we can achieve nested anonymous type functionality in c# based on our requirements.
In c#, anonymous types are most useful in the Select clause of LINQ query expressions to return the subset of properties from the defined object based on our requirements.
Following is the example of defining anonymous types in the Select clause of LINQ query expression in c#.
If you observe the above example, we created a class called Student with different properties. In the Main() method, we created an anonymous type object in LINQ Select query expression to return only the required fields in the result set.
When you execute the above example, you will get the result as shown below.
In c#, while creating an anonymous type object, we must provide the name for a property that is being initialized with an expression. If we don’t specify the field names in LINQ Select query expression while creating an anonymous type, then the compiler will automatically consider the same name as the property being used to initialize them, as shown below.
To learn more about LINQ query expressions, refer to LINQ Tutorial.
The following are the important points which you need to remember about anonymous type in c#.