Optimize Namespace References
- 2 minutes to read
Purpose
Removes unused references in the “using” (C#) or “Imports” (Visual Basic) section and optionally sorts them in the active source file.
Note
You can also run the Optimize Namespace References refactoring in XAML to remove unused xmlns references. This refactoring does not support reference sorting in XAML.
Availability
Available when the cursor is in a using (Imports) statement if the source code file contains unused references.
Usage
Open the Editor | All Languages | Namespace References options page to configure this refactoring. For example, set the “Sort namespace references” option to “Alphabetically”.
If you want to disable automatic sorting, uncheck the “Apply sorting when optimizing references” option.
You can also prevent the “Optimize Namespace References” refactoring from removing certain unused references which you want to leave in a file. For this, add these references to the “Never remove following references” list. This list contains “System” and “System.Linq” references by default.
For more information about the “Optimize Namespace References” options, refer to the following topic: Namespace References Settings.
Place the caret in a using (Imports) statement.
Press the Ctrl + . or Ctrl + ~ shortcut to invoke the Code Actions menu.
Select Optimize Namespace References from the menu.
After execution, this refactoring sorts the namespace references alphabetically and removes unused references except System and System.Linq.