Declarations
To wrap up the discussion about well-known statements, we should consider declarations, too. The declaration of a local variable consists of a type and a name specified using an identifier:
int age;
Initial assignment can be carried out at the same time but is optional. All that’s required for a declaration is a name and a type, unless local variable type inference (var) is used. In that case, the compiler needs to see a right-side expression from which to infer the type:
var name; // This is invalid.var name = "Bart"; // Here we can infer System.String.
It can’t be stressed enough that local variable type inference is just that: inference. The variable is still completely statically typed; we just reduce the amount of physical ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access