Sunday, September 4, 2011

Anonymous Types in C# 3.0

C# 2.0 gave us Anonymous methods which are declared inline and had no formal name, in the same way C# 3.0 gives Anonymous Types which are created with the help of combining ‘var’ (Local Variable Type Inference) and Object Initializers as follows:
 
      var cust = new { CustID = 101, CustName = "Manish" };
 
For the above given code, C# compiler automatically generates a name. Each member of the type i.e. CustID and CustName are the properties taken from the syntax of Object Initializer.