Rules for using Anonymous Types in C#

Published on June 21, 2015 by abundantcode

Filed under LINQ (General)

Last modified June 21, 2015

Print this page

rate 1 star rate 2 star rate 3 star rate 4 star rate 5 star
Your rating: none, Average: 0 (0 votes)

This article have been viewed 2016 times

The anonymous types (var) in C# is an interesting feature that the C# developers could use within their .NET application . When using the anonymous type , there are certain criteria or rules that needs to be followed .

Rules for using Anonymous Types in C#

Below are some of the rules that the developers should be following when using the anonymous type.

1. NULL value cannot be assigned to the anonymous type. When you try doing that , you will receive the following error “Error    CS0815    Cannot assign <null> to an implicitly-typed variable”.

2. Anonymous type cannot be used for the class member or fields.

3. Anonymous types are also derived from the Object type.

4. You cannot have the anonymous type as the return type of a method.


5. You cannot have the anonymous type as the parameter for a method.

6. The anonymous type or implicit type variable has to be initialized when declared.

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

Leave a Comment


You might be Interested in these Posts

How to Get the Top 5 elements from a List in C# ?

If you want to get the top 5 elements from a list in C# or skip the first item and get the next five...

Example of Anonymous Type Indexes in For Loop in C#

The anonymous type (var) can be used to initialize the indexes in a for loop or the object within th...

Anonymous Types are read-only in C#

If you try to set a value to the anonymous type in C# , you will receive an compiler error . The pro...