JetBrains Rider 2024.1 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, and so on.

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

Configure 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.

  1. Press Ctrl+Alt+S or choose File | Settings (Windows and Linux) or JetBrains Rider | Preferences (macOS) from the menu , then choose Editor | Code Style | C# | Syntax Style on the left.

  2. Use preferences in the Reference qualification and 'using' directives group to fine-tune namespace import style:

    • Prefer fully qualified references — select this checkbox if you prefer using fully qualified names rather than namespace import directives for imported types.

    • Add 'using' directive to the deepest scope — if this checkbox is selected, namespace import directives are added inside the namespace where the imported types are used; otherwise the import directives are added at the top of the file.

    • Place 'System.*' and 'Windows.*' namespaces first when sorting 'using' directives — if this checkbox is selected, 'using' directives like using System.Linq; will be placed above other using directives when sorting. Otherwise, these directives will be sorted alphabetically with others.

    • Prefer fully qualified using name at nested scope — if this checkbox is selected, fully qualified names for imported types are preferred in nested types and namespaces.

    • Allow using alias directive — selecting this checkbox allows using aliases in namespace import directives, as opposed to fully qualified namespace names.

    • Allow the 'global::' prefix — if this checkbox is selected, the global:: prefix are not removed, as in global::System.String. For more information, refer to :: operator - the namespace alias operator.

  3. Click Save in the Settings dialog to apply the modifications and let JetBrains Rider choose where to save them, or save the modifications to a specific settings layer by choosing this layer from the Save selector. For more information, see layer-based settings.

Import 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, refer to Import missing namespaces.

Remove redundant namespace imports and optimize 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 Built-in: Full Cleanup profile or create and run 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.

Optimize namespace imports using Code Cleanup

  1. Press Ctrl+Alt+S or choose File | Settings (Windows and Linux) or JetBrains Rider | Preferences (macOS) from the menu .

  2. Go to the cleanup profiles settings page: Editor | Code Cleanup.

  3. Create a new profile as described in the Create a new custom cleanup profile 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 in the Settings dialog to apply the modifications and let JetBrains Rider choose where to save them, or save the modifications to a specific settings layer by choosing this layer from the Save selector. For more information, see layer-based settings.

  5. Select the scope where you want to optimize namespace imports:

    • Place 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. Press Ctrl+R, C or choose Code | Reformat and Cleanup… from the main menu .

  7. In the Reformat and Cleanup Code dialog that opens, select the newly created profile and choose another scope if needed. .

  8. Click OK. JetBrains Rider will optimize namespace imports in the selected scope.

If you want to optimize namespace imports without opening the Reformat and Cleanup Code dialog to choose a profile, you can bind the created profile to the silent cleanup and run it by pressing Ctrl+R, G. You can also create a custom cleanup profile that would combine optimizing namespace imports with other code style tasks.

To apply preferences for namespace imports together with all other formatting and syntax style rules to the selected code block, Alt+Enter and choose Reformat and cleanup | Reformat & apply syntax style.

You can optimize namespace imports in code that you have recently modified and are going to commit to Git. JetBrains Rider will run the selected cleanup profile before committing.

Optimize namespace imports before committing it to Git

  1. Press Ctrl+K or select Git | Commit from the main menu.

  2. In the Commit tool window, click Show Commit Options and in the Commit Checks area, select the Optimize imports checkbox.

  3. Click Commit or Commit and Push. JetBrains Rider will remove redundant namespace imports in files staged for the commit, and then commit the changes.

You can optimize namespace imports every time you save changes in a file to make sure that your edits always comply with your code style. Note that this will only happen when you save changes explicitly with Ctrl+S or Ctrl+S and will not be triggered by auto-saving. However, all auto-saved files are placed to the 'reformat and cleanup' queue and will be processed on the next explicit save.

Automatically optimize namespace imports on saving changes

  1. Press Ctrl+Alt+S to open the IDE settings and then select Tools | Actions on Save.

  2. Select Reformat and Cleanup Code, choose and whether to apply it to the whole file or only to the changed lines.

  3. The next time you finish editing and save the file or all files , JetBrains Rider will clean up the affected files using the selected profile.

Last modified: 11 February 2024