Thursday, September 16, 2010

Anonymous Types

Anonymous types:  As the name itself clears, creates an object without declaring its data type/class. Since name of the data type/class is not specified --that the type is referred to as an anonymous type.

VB.Net
Person = New With {
    .FirstName = "Ram",
    .MiddleName = "Gopal",
    .LastName = "Verma" }
C#
 var Person = New With {
    FirstName = "Ram",
    MiddleName = "Gopal",
    LastName = "Verma" };
Above example is one of  declaration of anonymous typed object where once a person object is created then this object is used as if object of some of real physical class.

During compilation while the control reached at above code then the following are the steps that are performed internally.
  1. Compiler automatically generates anonymous class.
  2. It anonymously instantiate an object that contains the properties in anonymous type
  3. The instantiated object is assigned to the anonymous type variable "Person" 
 The anonymous type inherits from Object, it holds the properties specified in declaring the object.

So object of anonymous  type are used as of a normal class.

No comments: