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.
In this topic:
- Configuring namespace imports preferences
- Importing missing namespaces
- Removing redundant namespace imports and optimizing existing ones
Configuring namespace imports preferences
Your namespace imports preferences are saved using the mechanism of shared 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
- Open the page of ReSharper options.
- On this options page, you can specify namespaces that should never be removed and/or namespaces that should be always imported.
- Open the page of ReSharper options.
- Use the options in the Reference qualification group to fine-tune namespace import style.
- Click Save to apply the modifications and let ReSharper 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 Managing and Sharing ReSharper Settings.
Importing missing namespaces
ReSharper helps you detect symbols with missing namespace imports and add proper imports automatically. An unresolved symbol is immediately highlights in the editor and the corresponding fix is suggested:
Removing redundant namespace imports and optimizing 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:
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
- Open the Code Cleanup options: .
- Create a new profile as described in the Code Cleanup section. In the Selected profile settings for the new profile:
- Click Save to apply the modifications and let ReSharper 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 Managing and Sharing ReSharper Settings.
- Select the scope where you want to optimize namespace imports:
- Do one of the following:
- In the Code Cleanup dialog that opens, select the newly created profile.
- Click Run. ReSharper will optimize namespace imports in the selected scope.
If you want to optimize namespace imports without opening the Code Cleanup dialog, you can bind the created profile to the silent cleanup and run it simply by pressing Ctrl+Shift+Alt+F. You can also create a custom cleanup profile that would combine optimizing namespace imports with other code style tasks.
This feature is supported in the following languages/technologies:
The instructions and examples given here address the use of the feature in C#. For details specific to other languages, see corresponding topics in the ReSharper by Language section.
See Also
Procedures:
- Naming Style
- Managing and Applying Code Formatting Rules
- File and Type Layout
- File Header Style
- Code Syntax Style: Namespace Imports
- Code Syntax Style: Implicit/Explicit Typing
- Using Auto-Properties
- Making Fields Readonly
- Code Syntax Style: Multiple Attributes
- Code Syntax Style: Modifiers
- Code Syntax Style: Optional Member Qualifiers
- Reformatting XML Doc Comments
- Removing Code Redundancies