O'Reilly logo

C# 5.0 Unleashed by Bart De Smet

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required