dotnetspider.com


 


TutorialsForumResourcesReviewsJobsInterviewVideosCommunitiesProjectsTraining

Subscribe to Subscribers


Online MembersSunitha
Prasad kulkarni
Prabu Thangavelu
chaminda
sravan
cloud
Shesh Kumar Mishra
Gajanan
pavan
Anu George
somasekar
More...




Resources » Articles » .NET Framework


Anonymous Types in LINQ : A Step Ahead Series


Posted Date:     Category: .NET Framework    Rating: 4 out of 54 out of 54 out of 54 out of 5
Author: Member Level: Gold    Points: 18


Introduction:

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.

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.


  • 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

    example:
    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.

example:

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

Reference http://www.msdotnetheaven.com/?p=411


Did you like this resource? Share it with your friends and show your love!





Responses to "Anonymous Types in LINQ : A Step Ahead Series"
Author: Miss Meetu Choudhary    06 Feb 2009Member Level: Gold   Points : 0
hi

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



Feedbacks      

Post Comment:




  • 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:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Next Resource: Creating A Log File
    Previous Resource: Idea about cookie session
    Return to Resources
    Post New Resource
    Category: .NET Framework


    Post resources and earn money!
     
    More Resources
    Popular Tags   Tag posting guidelines   Search Tags  
    Anonymous Types in LINQ : A Step Ahead Series  .  

    My Profile

    Active Members
    TodayLast 7 Daysmore...


    Awards & Gifts


    Email subscription
  • .NET Jobs
  • .NET Articles
  • .NET Forums
  • Articles Rss Feeds
    Forum Rss Feeds



    About Us    Trademark Disclaimer    Contact Us    Copyright    Privacy Policy    Terms Of Use    Revenue Sharing sites   Advertise   Talk to Tony John
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
    2005 - 2012 All Rights Reserved.
    .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
    Articles, tutorials and all other content offered here is for educational purpose only.
    We are not associated with Microsoft or its partners.