Labels

Tuesday, April 29, 2008

Anonmous Types - Orcas C# New Features

Hi,

This blog post summarizes the new features of C# langauge shipped with Orcas (VS 2005).

Below are the new features been introduced.

§ Automatic Properties

§ Object Initializers

§ Collection Initializers

§ Extesnion Methods

§ Lambda Expressions - p => expressions

§ Query Syntax

§ Anonymous Types

· Concisely define inline CLR types within code, without having to explicitly define a formal class declaration of the type.

· Particularly useful when querying and transforming/projecting/shaping data with LINQ.

§ Var Keyword

Anonymous Types

· Anonymous types are a convenient language feature of C# and VB that enable developers to concisely define inline CLR types within code, without having to explicitly define a formal class declaration of the type.

· Anonymous types are particularly useful when Querying and Transforming/Projecting/Shaping data with LINQ. Refer Scott G Post.

Note:

· There is absolutely no difference from a CLR perspective between an anonymous type and an explicitly defined/named type. Anonymous types are purely "syntactic sugar" that avoid you having to type code - the runtime semantics are the same as using explicitly defined types.

· The CLR itself actually doesn't know the difference between an Anonymous Type and a Named Type - so the runtime semantics of the two are absolutely identical.

· The actual CLR name of the anonymous type will automatically be generated by the C# compiler.

· Bart De Smet has a good blog post here that details this if you want to see the exact class name pattern and IL generated.

· This means that all of the standard .NET type reflection features work with anonymous types - which means that features like databinding to UI controls work just fine with them.

Example of using Anonymous Types in LINQ –

While instantiating anonymous types required is just to leave the type-name blank after the "new" keyword

In the code above declared is an anonymous type, as part of the select clause within the LINQ expression. Here the compiler automatically create the anonymous type with 4 properties (Id, Name, UnitPrice and TotalRevenue) - whose property names and type values are inferred from the shape of the query.

Strongly-Typed Language Support:

Though the syntax gives the dynamic language-like flexibility, it also still retain the benefits of a strongly-typed language - including support for compile-time checking and code intellisense within Visual Studio. For example, notice above how the foreach loops over the returned products sequence and we are still able to get full code intellisense and compilation checking on the anonymous type with custom properties that was inferred from the LINQ query.

See great article on its internal working by Bard De Smet. You can read his excellent post on it here.

Reference: http://weblogs.asp.net/scottgu/archive/2007/05/15/new-orcas-language-feature-anonymous-types.aspx

Hope this helps.

Thanks & Regards,

Arun Manglick || Tech Lead


No comments:

Post a Comment