Search This Blog

Wednesday, February 15, 2012

Anonymous Type IN C# .NET3.0/4.0 Part1


Anonymous Types

Anonymous types provide a convenient way to encapsulate a set of read-only properties into a single object without having to explicitly define a type first. The type name is generated by the compiler and is not available at the source code level. The type of each property is inferred by the compiler.
In typical C# you will always carefully spell out your definitions:
string MyString = “Rashmi Kant”;
From the right side of the declaration it is obvious that only one type (string) will ever match this definition. So instead of us doing the work, why not let the compiler figure this out?
var MyString = “Rashmi Kant”;
The above definition will also create a string variable named “MyString”. It is important to note that C# is still strongly typed — unlike scripted languages such as Visual Basic (or PHP) once you have assigned the variable type it sticks. The following will not work:
var MyString2 = “Rashmi Kant”;
MyString2 = 123; //error
The compiler will throw an implicit conversion error as 123 cannot be assigned to a string.
The above was an impressive (if somewhat pointless) example of what an anonymous type is. For simple types such as strings, integer etc anonymous types offer little benefits.
You can create an array of anonymously typed elements by combining an implicitly typed local variable and an implicitly typed array, as shown in the following example.
var anonArray = new[] { new { name = "apple", diam = 4 }, new { name = "grape", diam = 1 }};
Other Example of anonymous class
var person0 = new
{
    FirstName = "Rashmi",
    LastName = "Kant",
    Height = 172
};
var person1 = new
{
    FirstName = "John",
    LastName = "Doe",
    Height = 165
};
var person2 = new
{
    LastName = "Doe",
    FirstName = "John",
    Height = 175
};
var Employee = new { ID = 5, Name= "Rashmi" };
 
Projection in  Anonymous types
Anonymous types also support Projection. So, if we have a declaration as shown below 
var LastName = "Nurk";
var FirstName = "Fred";
var person4 = new { LastName, FirstName};
then a new Anonymous type will be created with the read only properties LastName and FirstName. C# automatically projects the names of the variables to the names of properties in the anonymous class.
This also works while using objects, as shown below –
Person personObject = new Person
{
    LastName = "Doe",
    FirstName = "Jane",
    Height = 156
};
var projectionFromClass = new
{
    personObject.FirstName,
    personObject.LastName
};
In this case, the object projectionFromClass will have an anonymous type that picks up the property names FirstName and LastName, which will hold the values “Jane” and “Doe”.
Usage rules/restrictions
The properties in Anonymous types are all read only and therefore cannot be modified once they are created.
Anonymous types cannot have methods.
Anonymous types are always assigned to vars. This allows the compiler to assign the right type. But, if Anonymous types are used as return values or as parameters in a function, they will have to be passed in as Objects, as var is not a proper type.

No comments :

Post a Comment