Any previously imported namespace can become redundant in your code. Without ReSharper, you have to stop what you are doing, scroll to the top of every file, find unused namespace import directives, and remove them. ReSharper makes life a lot easier by automatically removing unused namespace import directives from your file, project or solution, at any time. This can be done either with a quick-fix or using Code Cleanup.
By default, ReSharper does not optimize using directives in generated code (for example, in Windows Form Designer-generated code). If necessary, you can edit the list of files and regions containing generated code in the ReSharper | Options | Code Inspection | Generated Code.
- Choose ReSharper | Options in the main menu.
- Open ReSharper | Options | Code Editing | C# | Namespace Imports (for C# settings) or ReSharper | Options | Code Editing | Visual Basic .NET | Namespace Imports (for VB.NET settings).
- Modify settings in these pages as required to comply with your coding practices/standards.
- Open the Code Cleanup options: ReSharper | Options | Code Editing | Code Cleanup.
Create a new profile as described in
Creating Custom Profiles.
Selected profile settings for the new profile:
- Select Optimize 'using' directives.
- Alternatively, 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.
- Click Save to apply the modifications and let ReSharper choose the settings layer, or choose a particular settings layer in the Save To drop-down list. To learn more about saving ReSharper settings, see JetBrains .NET Tools Blog: Save or Save To in ReSharper Options.
- Choose ReSharper | Tools | Cleanup Code in the menu to open the Code Cleanup dialog box for the current file or for a solution node (see Running Code Cleanup for details), and then select the newly created profile in the Available Profiles area.
- Click Run to perform Code Cleanup. ReSharper will delete all redundant namespace import directives in the current file of in the selected solution node.
You can also optimize using directives in the specific scope with the Fix in Scope.
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 the corresponding topics in the ReSharper by Language section.