Building blocks and Data sources of LINQ - Part 3
In the last few posts, I coverted few C# 3.0 features namly Object initialization Expressions, Extension Methods and Lamda Expressions and Expression Trees. In this post, I will cover the concept of Anonymous Types and Implicitly Typed Local Variables in details.
5. Anonymous Type
An anonymous type is a type that is declared without an identifer. You can use object initalizers without specifying the class that will be created with the 'new' operator. The compiler creates the anonymous type at compile time and not run time. Anonymous types are particularly useful when querying and transforming/projecting/shaping data with LINQ
let's we take an example of an Employee class with three properties namly Name, Salary and EmpId and see how we declare and instantiate an anonymous type for Employee class,
var employee = new { Name = "Arjun", Salary = 3000.5, EmpId=339 };
At compile time, the compiler creates a new (anonymous) type with properties inferred from the object initializer. Hence, the new type will have the properties Name, Salary and EmpId.
The Get and Set methods, as well as the corresponding private variables to hold these properties, are generated automatically. At run time, an instance of this type is created and the properties of this instance are set to the values specified in the object initializer.
It converts the above code in the background as below
class __Anonymous1
{
private string name ;
private decimal salary;
private int empId;
public string Name{ get { return name; } set { name = value ; } }
public decimal Salary{ get { return salary; } set { salary= value ; } }
public int EmpId{ get { return empId; } set { empId= value ; } }
}
__Anonymous1 employee = new __Anonymous1();
employee .Name = "Arjun";
employee .Salary =3000.5;
employee .EmpId =339;
Let we another sample with multiple Anonymous type declaration.
var Customer = new{
Company = "Photon Company",
Name = "Arjun",
EmpId = 339,
Contacts = new {
Phone = "(044)3141231",
Email = "abc@mail.com" }
};
Notice the Contacts member which is another anonymous type nested inside the first type. Without anonymous types, LINQ would not be able to create types dynamically. This is what allows LINQ to query for an arbitrary set of data, without first having the structure declared.
5. Implicitly Typed Local Variables
A new keyword, 'var', has been added to C#. When the compiler sees it, it implicitly defines the type of the variable based on the type of expression that initializes the variable.
Let we see how its working in the following samples,
var i = 5; // is equivalent to int i = 5;
var s = "this is a string"; // is equivalent to string s = "this is a string";
An implicitly typed local variable must have an initializer. For example, the following declaration is invalid:
var s; // wrong definition, no initializer.
Note :
- As you can imagine, implicit typing is really useful for complex query results because it eliminates the need to define a custom type for each result.
- Implicitly typed local variables cannot be used as method parameters.
Happy Coding!!!