JetBrains Rider 2017.2 Help

Code Syntax Style: Namespace Imports

In each of your source code files, you should have the right set of namespace imports (using directives in C#). If an import is missing, your code does not compile, if there are unused imports, they could make maintaining your code difficult over time. Moreover, there are different approaches to importing namespaces, for example, you can either add using directives or use fully qualified type names, you can place using directives on top of the file or inside a namespace, etc.

ReSharper provides a number of features to help you control and automatically update all aspects of importing namespaces according to your preferences.

Configuring namespace imports preferences

Your namespace imports preferences are saved using the mechanism of layer-based settings. Among other things, this mechanism allows you to maintain different preferences for different solutions as well as to keep these preferences under a VCS and automatically share them with your team members.

To configure namespace imports preferences

  1. Open the Code Editing | C# | Code Style page of JetBrains Rider options.
  2. Use the options in the Reference qualification group to fine-tune namespace import style.
  3. Click Save to apply the modifications and let JetBrains Rider choose where to save them, or save the modifications to a specific settings layer using the Save To drop-down list. For more information, see layer-based settings.

Importing missing namespaces

JetBrains Rider helps you detect symbols with missing namespace imports and add proper imports automatically. An unresolved symbol is immediately highlighted in the editor and the corresponding fix is suggested:

Namespace import quick-fix
If there are several unresolved symbols in the file, the fix adds imports for all of them. The imports are added according to the configured preferences of namespace imports. For more information, see Importing Missing Namespaces.

Removing redundant namespace imports and optimizing existing ones

Any imported namespace becomes redundant in your code if you remove usages of namespace symbols. Without JetBrains Rider, you have to scroll to the top of every file, find unused namespace import directives, and remove them. JetBrains Rider makes life a lot easier by allowing you to automatically remove unused namespace import directives from your file, project or solution. Besides removing redundant namespace imports, JetBrains Rider can optimize them in several ways according to your namespace imports preferences.

All redundant namespace imports are highlighted are the editor and the corresponding quick-fixes are suggested including fix in scope:

Unused namespace import quick-fix
By applying suggested fixes you can remove redundant namespaces instantly in the desired scope.

Another option to remove redundant namespace imports in a bulk mode is code cleanup. You can either run code cleanup with the default profile Default: Full Cleanup or run the cleanup with a custom profile solely targeted at your specific task as described below.

In contrast to quick-fixes, code cleanup not only removes unused imports but also optimizes existing imports according to your preferences. For example, using the code cleanup you can replace fully qualified type names with plain type names by adding the corresponding namespace imports.

To optimize namespace imports using Code Cleanup

  1. Press Ctrl+Alt+S, or alternatively choose File | Settings on Windows and Linux or Rider | Preferences on macOS.
  2. Go to the Code Cleanup settings page: Editor | Code Cleanup.
  3. Create a new profile as described in the Code Cleanup section. In the Selected profile settings for the new profile:
    • Select Optimize 'using' directives.
    • Optionally, to embrace unused using directives in a region, select Embrace 'using' directives in region and specify the name of the region in the Region name field.
  4. Click Save to apply the modifications and let JetBrains Rider choose where to save them, or save the modifications to a specific settings layer using the Save To drop-down list. For more information, see layer-based settings.
  5. Select the scope where you want to optimize namespace imports:
    • Set the caret anywhere in the file to optimize namespace imports to the file.
    • Select one or more items in the Solution Explorer to optimize namespace imports in the files under these nodes and their child items.
  6. Choose Code | Code Cleanup in the main menu.
  7. In the Code Cleanup dialog that opens, select the newly created profile.
  8. Click OK. JetBrains Rider will optimize namespace imports in the selected scope.
Last modified: 21 November 2017