Anonymous Types in LINQ : A Step Ahead Series


Programers of Visual Basic the keyword 'var' is confusing here as the type variants was used in Visual Basic.

Here, the keyword var tells to compiler emit a strong type based on t5e value of the operation on the right side.

Anonymous types can be used to initialize simple types like integers and strings.

Rules : Following are the some basic rules to use LINQ Anonymous Types.

  • Anonymous types can't be null.

  • Must always have an initial assignment.

  • Can be used with Simple or complex types, but composite anonymous types require member declaration

    var mylist = new {Topic="Anonymous-Types" [, declaratory = value, ...]}.

    in above Topic is the member declaratory.

  • Supports intelliSense.

  • Cannot be used for class field.

  • Can be used with arrays.

  • Anonymous types are all derived from the Object type.

Now, lets explore some category or types of anonymous types:

Simple Anonymous Type

With the keyword var and giving the value of the variable in the right side of the assignment operator (=), anyone can declare the Simple anonymous type.

var list = "Anonymous Types in LINQ : A Step Ahead Series";

The anonymous type is assigned to the name on the left side of the assignment operator, and the type emitted by the compiler to Microsoft Intermediate Language (MSIL) is determined by the right side of the operator.

The above line is identical in the MSIL if defined as following:

var list = "Anonymous Types in LINQ : A Step Ahead Series";

Array Initializer Syntax

Anyone can use the Anonymous Type to initialize the array too but with a rigid rule that is : new keyword must have to use.


var myArrayWithAntype = new int[]{ 1, 2, 12, 53, 58, 8, 2113, 2221 };

In above the array is Anonymous as it is defined with Annonymous Type initialize rule.

Composite Anonymous Types : Lets define

Now, lets take a look how to define Composite Anonymous Type. Its called just as class without the "typed" class definition.

var fullname = new {FirstName=”Gaurav”, LastName=”Arora”};

in above 'fullname' contains both first and last name. Go through following:

//throws an error

//Displays fullname

Note :
1. Anonymous types are immutable.
2. Within the scope of article we have supplied a little tricks, there can be generic anonymous methods, you can apply methods, can return anonymous values etc.


Article by Gaurav Aroraa
Gaurav is a Microsoft Technology Specialist professional. He has awarded lifetime membership from Computer Society of India (CSI). He has more than 13yrs of experience in the industry. Currently, he is working in the capacity of Solution Architect with an MNC. He is serving to the various communities since 1999.

Follow Gaurav Aroraa or read 157 articles authored by Gaurav Aroraa


Author: Mrs. Meetu Choudhary Nanda06 Feb 2009 Member Level: Gold   Points : 0


very good job, its more techie stuff it you ll define rest part.

Author: Phagu Mahato26 Jan 2014 Member Level: Gold   Points : 7

Anonymous types typically are used in the select clause of a query expression to return a subset of the properties from each object in the source sequence.Anonymous types contain one or more public read-only properties. No other kinds of class members, such as methods or events, are valid.

var vr = new { Fee = 108, Message = "Paid" };
Console.WriteLine(vr.Fee+ vr.Message);

The expression that is used to initialize a property cannot be null, an anonymous function, or a pointer type.
var productQuery =  from prod in Schools
select new { prod.Color, prod.Price };

foreach (var v in SchoolQuery)
Console.WriteLine("Color={0}, Price={1}", v.Roll, v.Fee);
Anonymous types have been introduced to support one of the most useful features called LINQ. It's most useful when you are querying collection of object using LINQ query . Below example about Anonymous types easily by using new keyword of C#
var Phagu = new { id=1, Name= "Phagu Mahato" };
var Prem = new { id=1, Name= "Prem" };

Array of anonymously typed elements by combining an implicitly typed local variable and an implicitly typed array, as shown in the following example code snippet.

var Arraytype = new[] { new { name = "Mango", diam = 4 }, new { name = "Coconut", diam = 1 }};

Author: Gaurav Aroraa30 Jan 2014 Member Level: Gold   Points : 0

@Phagu - Thanks for snippet, I have already explained all the things in this article. What part initiated you to write snippet again.

Author: Umesh Bhosale27 Mar 2014 Member Level: Silver   Points : 2

This is good article.
Anonymous type i.e. Var is used for generate class at runtime also..
var emp = new {empid=10;empname="Umesh"};

it will automaticaly create emp class like this

class emp
public int empid{get;set}
public sting empname{get;set;}

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: