Home > Code Generation > Declaring locals using CodeRush consume-first declaration features

Declaring locals using CodeRush consume-first declaration features

December 16th, 2011

Local variable declarations are the most often used types of declarations in code. A local variable is a type of variable declared by local variable declaration inside a block the variable is intended to be local to. The local variable declaration explicitly defines the type of the variable that has been declared along with the identifier that names the variable. You can also declare implicitly typed local variables, whose type is inferred by the compiler from the expression.

With the help of CodeRush consume-first declaration features, you don’t have to type the definition of a local variable declaration by hand. Any local reference to an undeclared element, or a statement that returns a value, but does not assign it to anything, can be automatically declared using consume-first declaration features specific to local variable declarations: Declare Local and Declare Local (implicit).

Before you apply any of the code providers, you can see the resulting code that is being inserted.

The Declare Local code provider creates an explicitly-typed local variable declaration, and the Declare Local (implicit) declares an implicitly-typed local variable declaration. The Declare Local defines the correct type of the new variable declaration from the code:

CodeRush Declare Local at reference preview

If there are several possible types, you can choose the type from a sub menu:

CodeRush Declare Local sub menus preview

If the Declare Local is performed on a variable reference, it declares a named variable on the scope where it is visible for all other variable references. If the code provider is executed on a statement, it’s value is assigned to a variable declaration:

CodeRush Declare Local on statement preview

The Declare Local (implicit) is similar to the Declare Local code provider, but it creates an implicitly-typed local variable declaration in case you don’t care about variable’s type:

CodeRush Declare Local (implicit) preview

See also: consume-first declaration features for field variables.

Products: CodeRush Pro, CodeRush Xpress
Versions: 11.2 and up
VS IDEs: any
Updated: Dec/16/2011
ID: C145

Similar Posts: