Getting started with LINQ
Software Development September 24th, 2007
New post about Linq in my blog:
New post about Linq in my blog:
C# 3.0 has many-a-new features. This post will explain the ’var’ keywork and the concept of Implicitly Typed Variables.
The var keyword is not a late-bound or un-typed variable reference. The var keyword always generates a strongly typed variable reference. The main idea is that the developer is not required to define the type of the variable at the time of declaration, but it is the task of the compiler to decide what type of the object the variable is. The compiler infer the type of the variable from the expression used to initialize the variable when it is first declared.
Important points about var keyword:
What rules shuold you force to use the var keyword?
When you declare variable with the var keyword, you must to always do an initial value assignment. This is because the var keyword produces a strongly-typed variable declaration. Hence, the compiler needs to be able to infer the type to declare based on its usage. If you don’t, the compiler will produce a compiler error.
1: var vAge = 32;
2: var vHeight = 175.2;
3: var vName = “Maor David“;
The compiler will infer the type of the “age”, “height” and “name” variables based on the type of their initial assignment value. This means it will generate IL that is absolutely identical to the code below:
1: int age = 32;
2: double height = 175.2;
3: string name = "Maor David";
You can also use it to other data types:
1: foreach (var vTable in ds.Tables)
2: {
3: foreach (var vRow in ((DataTable) vTable).Rows)
4: {
5:
6: }
7: }
This is equal to:
1: foreach (DataTable table in ds.Tables)
2: {
3: foreach (DataRow vRow in table.Rows)
4: {
5: }
6: }
You can read this post also at Maor David’s Blog: http://blogs.microsoft.co.il/blogs/maordavid/archive/2007/09/15/C_2300_-3.0-var-keyword.aspx
C# 3.0 includes a new language feature called object initializers.
Object initializers enable you to initialize the state of an object with a single expression that does not require use of parameterized constructors.
Basically, this feature allows you to initialize an object by both creating the object instance (i.e. a new expression) as well as assign values to one or more properties in a single sentence, or to initialize a collection with a set of values to add to it in a single expression.
class Program { static void Main(string[] args) { Customer cus1 = new Customer() { Name = "Maor", City = "Herzliya" }; Customer cus2 = new Customer() { Name = "Guy", City = "Ramat Hasharon" }; List<Customer> customers = new List<Customer>() { cus1, cus2 }; } } class Customer { public string Name { get; set; } public string City { get; set; } }
The first & the second lines initialize a Customer object with the default constructor (though you could call a constructor with parameters here), and assigns explicit values to the Name and City properties.
The third line initializes a collection of type List<Customer> with the objects cus1 and cus2.