ReSharper
 
Get ReSharper
Get your hands on the new features ahead of the release by joining the Early Access Program for ReSharper 2025.1! Learn more

Code inspection: Local variable has too wide declaration scope

Last modified: 08 April 2024

ReSharper suggests moving the declaration of a local variable closer to its usage. If a variable is not used outside a block of instructions, it is a good practice to declare it within that block.

In the example, ReSharper suggests moving the declaration of tmpvar inside the for loop, where tmpvar is used.

If possible, ReSharper will automatically join the declaration and assignment, and according to the default preferences will change the explicit variable type to var when it applies this quick-fix.