What are anonymous types in C #?

Possible duplicate:
How to use anonymous types in C #?

What are anonymous types in C # and when should they be used?

+2


a source to share


2 answers


Anonymous types are types created on the fly, typically to return results to a LINQ statement. Here is an example from MSDN

var productQuery = 
    from prod in products
    select new { prod.Color, prod.Price };

      

A new type is created with read-only Color and Price properties, and the query returns instances of that type when enumerated.



foreach(var product in productQuery) {
    Console.WriteLine(product.Color);
}

      

product

will be of the anonymous type defined above.

Anonymous types are useful for returning multiple properties from a query without having to explicitly specify the type for this purpose.

+3


a source


Straight from the horse's mouth: http://msdn.microsoft.com/en-us/library/bb397696.aspx



+2


a source







All Articles