Sunday, April 11, 2010

Anonymous Types in .net

Anonymous Types (var name) :

  • Concisely define inline CLR types within code

  • No need to explicitly define a formal class declaration of the type.

  • Useful when querying and transforming/projecting/shaping data with LINQ.


var resultSet = from details in db.projectRequest_details
where details.rname.StartsWith("D")
select new
{
name = details.rname,
utaid = details.rutaid
};




  • the var keyword always generates a strongly typed variable reference.

  • var keyword instead tells the compiler to infer the type of the variable from the expression used to initialize the variable when it is first declared.

  • It is not possible to define a variable using "var" key word like var userName;, you have to declare it using some value like var userName="Quazi Mainul Hasan";

No comments:

Post a Comment