Anonymous type Object Initializers
What is Anonymous Type of Object Initializers in .NET
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Hemant KumarRank #1,202 · 1.5K reputationPosted Oct 19, 2011, 8:24 PM EDT
Anonymous types are a new language feature introduced in the C# 3.0 release. For those that remember your set theory, an anonymous type is described as a tuple type that is automatically inferred and created from its object initializer. An object initializer specifies the values from one or more fields (or properties) of an object. Another way to look at this is that the object initializer specifies the named parameters that passed to an object.
All of this happens at compile time, so anonymous types are still strongly typed. In reality, the compiler automatically creates a class that represents the anonymous type. For example,
var product =new{Name ="C# Rocks!", Price = 3};automatically gets translated by the compiler to something that looks likethis:class__Anonymous1{privatestringname;privateintprice;publicstringName{get{returnname; }set{ name = value; }}publicintPrice{get{returnprice; }set{ price = value; }}}__Anonymous1 product =new__Anonymous1();product.Name ="C# Rocks!";product.Price = 3;While anonymous types might not seem all that useful on the surface, they are the cornerstone for LINQ. Without anonymous types, LINQ would not be able to create types dynamically. This is what allows LINQ to query for an arbitrary set of data, without first having the structure declared.
refer this links
http://www.dotnetfunda.com/articles/article1479-object-initializers-in-net-framework-.aspx
http://weblogs.asp.net/dwahlin/archive/2007/09/09/c-3-0-features-object-initializers.aspx