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

  1. Open the Editor | All Languages | Namespace References options page to configure this refactoring. For example, set the "Sort namespace references" option to "Alphabetically".

    SortByName

    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.

  2. Place the caret in a using (Imports) statement.

    Example

  3. Use the Ctrl + . or Ctrl + ~ shortcut to invoke the Code Actions Menu.

  4. Select Optimize Namespace References from the menu. Choose

After execution, this refactoring sorts the namespace references alphabetically and removes unused references except System and System.Linq.

Result

See Also