Cyrus has a good post explaining the new var data type in C# 3.0 (which debuted in the LINQ demonstrations). At this point it doesn’t seem more than some syntactic sugar to save the amount of keystrokes needed for complex local variable declarations…anyone coming from a VB “classic” background (or javascript for that matter) will also automatically correlate this type with the generic variant (or var) type in those languages. The big difference is that the C# 3.0 var type is checked at compile time (thus needing both a declaration and an assignment on the same line), whereby the legacy types are checked at runtime (and thus have quite a bit of overhead)…so similar in name only.
The code examples that Cyrus gives do a pretty good job of summing up the advantages of the implicit compile time type checking that var attempts to solve, and I do agree with him about the improved readability, though I’m sure initially it looks a bit foreign as we’re all used to seeing an explicit primitive (or some other readily understandable type declaration)…but after looking at it for a bit it’s pretty apparent that in the scope of local variables it’s really not that important, and I’m sure intellisense will aid quite a bit. For heavily genericized type declarations, it appears that this will vastly simplify readability.



No comments yet