Tuesday, 14 February 2012

Anonymous Types

Anonymous types:

Anonymous types are introduced in C# 3.0 which permits object initializer to create anonymous type by using the ‘var’ keyword.

Anonymous type is a class which is created by CLR automatically at run time and this class is not visible by users.

These types can reduce the code that need to access the class properties.

Class does not need to define properties, CLR will taken at run time.

Example:

Class AT

{

Static void main (string[] args)

{

Var i=new { no= 1 , name= “ ravi”, phoneno = 9985585867};

Console. Write ( i. no + “ “ +i. name + i.phoneno );

Console.ReadLine ();

}

}

In above example,we use ‘var’ keyword to define properties for class AT in single line of code .so anonymous types are used to define properties of class in simple manner.

No comments:

Post a Comment