Code Issues hints for declaration and initialization

Here are a few simple code issues of a hint type (suggestion) for declarations and initializations.

Can combine initialization with declaration

Cause:

The declaration and its initialization can be combined into a single statement instead of two separate statements. Combining an initialization and declaration may improve code readability.

Sample:

How to fix:

Apply the Move Initialization to Declaration refactoring:

Can inline temporary variable

Cause:

The expression assigned to the temporary variable that is used only once can be inlined. Removing a temporary variable may improve code readability.

Sample:

How to fix:

Apply the Inline Temp refactoring:

Declaration can be a constant

Cause: If the initialized declaration does not change its value, it can be a constant. A constant differs from a variable because once declared, the value assigned to the constant cannot be modified. Having constant declarations creates a self-documenting and strong code by telling the developer that he/she is not allowed to change the value of the constant variable, which can results in fewer errors. Additionally, constants improve performance over regular variables in many cases, particularly when using integral types such as integer.

Sample:

How to fix:

Apply the Convert to Constant refactoring:

Implicit variable can be used

Cause:

This code issue notifies a developer that an implicitly typed local variable can be used. Such variables provide a new simplified declaration syntax that instructs the compiler to infer the type of a new variable according to its initial usage. Converting a variable to an implicitly typed local variable may improve code readability.

Initializer can be used

Cause:

The instantiation of a variable and subsequent property assignments can be converted into a object initializer expression. Object initializers allow you to set the initial values of fields or properties of an object as part of the single instantiation statement, which is more compact and may improve code readability