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 ...

Get C# 5.0 Unleashed now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.