Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
This refactoring applies to:
C#
Visual Basic
What: Lets you move variable declarations closer to their usage.
When: You have variable declarations that can be in a narrower scope.
Why: You could leave it as it is, but that may cause readability issues or information hiding. This is a chance to refactor to improve readability.
How-to
Place your cursor in the variable declaration.
Next, do one of the following:
- Keyboard
- Press Ctrl+. to trigger the Quick Actions and Refactorings menu and select Move declaration near reference from the Preview window popup.
- Mouse
- Right-click the code, select the Quick Actions and Refactorings menu and select Move declaration near reference from the Preview window popup.
- Keyboard
When you're happy with the change, press Enter or click the fix in the menu and the changes will be committed.
Example:
// Before
int x;
if (condition)
{
x = 1;
Console.WriteLine(x);
}
// Move declaration near reference
// After
if (condition)
{
int x = 1;
Console.WriteLine(x);
}