CODEDIGEST
Home » Articles
Search
 

Technologies
 

CodeDigest Navigation
 

Technology News
No News Feeds available at this time.
 

Community News
No News Feeds available at this time.
 
Anonymous Types in LINQ

By Gaurav Arora
Posted On Mar 02,2009
Article Rating:
Be first to rate
this article.
No of Comments: 6
Category: C#
Print this article.

Anonymous Types in LINQ

 

Introduction

For Visual Basic programmers, the keyword 'var' is confusing here as the type variant was used in Visual Basic.

 

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

 

Important

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.

 

1.      Anonymous types can't be null.

2.      Must always have an initial assignment.

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

example

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

In above code, Topic is the member declaratory.

4.      Supports intellisense.

5.      Cannot be used for class field.

6.      Can be used with arrays.

7.      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";

 

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";

 



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.

 

example

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

 

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

 

Composite Anonymous Types : Lets define,

 

Now, lets take a look how to define Composite Anonymous Type. It’s 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

Console.WriteLine(fullname.FirstName);

 

//Displays fullname

Console.WriteLine(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.

Similar Articles
You can contribute to CodeDiget.Com:
Donate to CodeDigest.com
Article Feedback
Comments
Annonymous Type in LINQ - var
Hey Arturo,
Thanks for your words. But, I am disagree with you 'var' is not just meant for object initialize but much more in other we called as 'Implicitly Type variables'. The scope of article is upro Linq so, I have another post purely meant for 'var'. For more details please refer to msdn : http://msdn.microsoft.com/en-us/library/bb383973.aspx
Anonymous Types in LINQ
Just to be clear.
The keyword "var" is NOT in any way an Anonymous type declaration. The compiler doesn't create anonymous types just because you use var.
The keyword "var" vas introduce to make objects initialization simpler.
Example has error
Hey mark,
Thansk for your feedback. Please comment the line :
//throws an error

Console.WriteLine(fullname.FirstName);

It marked as error, this is for just understanding prspective.
Composite Anonymous Types - example has error
The output is:
Gaurav
{ FirstName = Gaurav, LastName = Arora }
Anonymous Types in VB.net
Hi Darrell,

First of all let me say thanks for your feedback, the code is written for everyone its not for language specific but this is in C#. Your idea about writting it in VB.net, I will work on this and try to publish all code in both C# and VB.net in future.
VB.NET Sample Code
Why not give equivalent VB.NET examples if you are trying to provide some understanding to VB programmers?