Translate

Wednesday 22 April 2009

Anonymous Types

Anonymous types are the most convenient way of working with those types, which do not need extensive usage or heavyweight creational structures.

The below code assigns a "var" type with a new instance whose type is/are the value(s) and their corresponding identifiers.

var implicitVar = new { Name = "A", Price = 3 };

So, if you do a

Console.WriteLine(implicitVar.ToString());

the output will be:

{Name="A", Price=3};

!!

The additonal advantage is that the variable "implicitVar" now knows the identifier's "Name" and "Price" as properties. So, you can do a:

implicitVar.Name and implictVar.Price

!!!

No comments: