ReSharper 2024.3 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.

ReSharper 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. Select ReSharper | Options from the main menu or press Alt+R O, then choose Code Editing | C# | Namespace Imports on the left.

  2. On this options page, you can specify namespaces that should never be removed and/or namespaces that should be always imported.

  3. Select ReSharper | Options from the main menu or press Alt+R O, then choose Code Editing | C# | Syntax Style on the left.

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

  5. Click Save in the Options dialog to apply the modifications and let ReSharper choose where to save them, or save the modifications to a specific settings layer by choosing this layer from the Save To selector. For more information, see manage and share resharper settings.

Import missing namespaces

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

This feature is supported in the following languages and technologies:

Language: C#

Language: VB.NET

Language: C++

Language: HTML

Language: ASP.NET

Language: Razor

Language: JavaScript

Language: TypeScript

Language: CSS

Language: XML

Language: XAML

Language: Resx

Language: Build Scripts

Language: Protobuf

Language: JSON

Feature is available in C#

Feature is available in Visual Basic

Feature is not available in C++

Feature is not available in HTML

Feature is available in ASP.NET

Feature is available in Razor

Feature is not available in JavaScript

Feature is available in TypeScript

Feature is not available in CSS

Feature is not available in XML

Feature is not available in XAML

Feature is not available in Resource files

Feature is not available in build script files

Feature is not available in Protobuf

Feature is not available in JSON

The instructions and examples given here address the use of the feature in C#. For more information about other languages, refer to corresponding topics in the ReSharper by language section.

Remove redundant namespace imports and optimize existing ones

Any imported namespace becomes redundant in your code if you remove usages of namespace symbols. Without ReSharper, you have to scroll to the top of every file, find unused namespace import directives, and remove them. ReSharper 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, ReSharper 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. Select ReSharper | Options from the main menu or press Alt+R O.

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

  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 Options dialog to apply the modifications and let ReSharper choose where to save them, or save the modifications to a specific settings layer by choosing this layer from the Save To selector. For more information, see manage and share resharper 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. Do one of the following:

    • Press Control+Alt+F or choose ReSharper | Edit | Cleanup Code… from the main menu .

    • Right-click anywhere in the text editor or right-click the selection and choose Cleanup Code in the context menu.

  7. In the Code Cleanup dialog that opens, select the newly created profile .

  8. Click Run. ReSharper will optimize namespace imports in the selected scope.

If you want to optimize namespace imports without opening the Code Cleanup dialog to choose a profile, you can bind the created profile to the silent cleanup and run it by pressing Control+Shift+Alt+F. 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 every time you save changes in a file to make sure that your edits always comply with your code style.

Automatically optimize namespace imports on saving changes

  1. Select ReSharper | Options from the main menu or press Alt+R O.

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

  3. Select and click Set as default (the default profile is also used for silent cleanup).

  4. Go to the Code Editing | Code Cleanup | General options page and select Automatically run cleanup when saving a file.

    Optionally, you can restrict automatic cleanup to specific files by a file mask.

    You can also select Only changed parts of file to make sure that cleanup is applied to the code affected by your changes, and the rest of the code in the file is not modified.

    If you choose to clean up only the changed parts of files, you can fine-tune this behavior with the following options:

    • Perform full cleanup if partial cleanup is not possible — depending on this option, ReSharper will either clean up the whole file or skip the file if it is not possible to restrict the cleanup to the modified parts of the file.

    • Allow cleanup on save for shared files — use this option to enable or disable partial cleanup in files that are included in the solution several times, such as files from shared projects.

  5. Click Save in the dialog to apply the modifications and let ReSharper choose where to save them, or save the modifications to a specific settings layer by choosing this layer from the Save To selector. For more information, see manage and share resharper settings.

  6. The next time you finish editing and save the file (Ctrl+S) or all files (Ctrl+Shift+S) , ReSharper will clean up the affected files using the selected profile. If the default cleanup profile is not selected, ReSharper will prompt you to choose one of the profiles.

Last modified: 26 May 2024