Implicitly Typed Local Variable Declarations
In the previous sections, you’ve seen a syntax used to declare and initialize local variables: specify the type, give the variable a name (lhs), and assign it some expression (rhs).
string name = "Bart";
Expressions, as you will see, always have a type. This means we have specified some redundant information to the compiler. On the left side, we called out a type explicitly, while the compiler could infer that information from the right side. In this case, the literal "Bart"
is of type System.String
. So, why do we need to specify that type explicitly on the left?
The var Keyword
Until version 2.0 of the C# language, the answer was just this: That’s the way it is; live with it. But starting with version ...
Get C# 5.0 Unleashed now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.