Wednesday, 8 September 2010

.NET: 3.5 - and NET 4.0 Feature

.Net 4.0:-http://blkarwasara.blogspot.com/2010/11/aspnet-40-features.html

In terms of Anonymous Types just about all that one needs to remember is right there, in the first paragraphs:

o Anonymous types provide a convenient way to encapsulate a set of read-only properties into a single object [of class type] without having to first explicitly define a type.
o The type name is generated by the compiler and is not available at the source code level.
o An anonymous type is limited to method scope.
o An anonymous type [...is directly derived from object].
o The type of the properties is inferred by the compiler [ie, no way to specify Type].
o No other kinds of class members such as methods or events are allowed.
o If you do not specify member names in the anonymous type, the compiler gives the anonymous type members the same name as the property being used to initialize them.
o You must provide a name to a property that is being initialized with an expression.


//Example A:
var v = new { Amount = 108, Message = "Hello" };

//Example B:
//Will create a variable called Amount as its specifically given,
//Will create a property called Name, using the same name as the source.
//But "Hello" will not be available as a prop, as it needed a property name:


Person p = new Person {Name="John", Age =30};
var v = new { Amount = 108, p.Name, "Hello" };