Search This Blog

2009-05-31

C# 3.0 Tutorial -1:Var Keyword

Var keyword

you can now declare a veriable with the "var" keyword where you dont need to mention the datatype

var Hello = "hello Programmer";var my number = 42;

At first glance, it appears that you’re declaring a variable of type “var”. Actually, "var" is not a type, but rather a new keyword that means, “I want to declare a variable, but I’m too lazy to write out its type”. For cases where the type is "Dictionary<int, List<int>>" or similar, this saves quite a bit of clutter in the code:// Before:Dictionary<int, List<int>> Coeffs = new Dictionary<int, List<int>>();// After:var Coeffs = new Dictionary<int, List<int>>();

Following is a code snippent of Var keyword (code is done within an aspx page of a WebSite application)