Due to my upbringing in C/C++ somehow I feel uneasy whenever I see some like
var a = 5;
I guess this is ok in scripting languages. So when I heard that C# 3.0 is going to support implicit types for local variables that made me feel uneasy. I installed the PDC C# 3.0 bits (from the link mentioned before) and tried the following
var a = 5;var s = "hello";Console.WriteLine(a.GetType());Console.WriteLine(s.GetType());
var s =
The output was as expected a was System.Int32 and s System.String. However C# 3.0 is supporting implicit types and not the variant data type that many non-typed languages support. So the following will not compile
var a = 5;a = "hello";
Since C# is a strongly typed language once the implicit type has been inferred during compilation it does not change.
While I do agree for things like LINQ implicit type is absolutely required but elsewhere as in the examples above vars make code more unreadable for me. Wherever I'll see a line of code with var I scroll up-down to figure out what type it exactly is. Specially in some situation the type of variable has a lot to do with performance so if I see some thing like
var al = Func1();........for (i = 0; i < 10000000; i++) foreach (int el in al) total += el;
I'd right click on al and say goto definition and see its a var. At this point its super important for me to figure out what's its type. If its an ArrayList and not a List<> then I have a huge perf degradation. Check out Rico Mariani's blog to find out why.
<edited some stuff like the example above to add clarity based on the comments>< Fixed some syntax errors >