ReSharper Help

Importing Missing Namespaces

When you use types whose namespaces have not been imported in the file, ReSharper helps you locate these types and add the missing namespace import directives. If there are several missing missing namespaces for unresolved types, e.g. after you paste a block of code in the file, ReSharper would import all these namespaces in a single action.

ReSharper looks for non-imported in the following assemblies: assemblies referenced in the current project, assemblies referenced in other projects in the solution, assemblies that are referenced transitively. If the type is found in an assembly that is not referenced in the current project, ReSharper also suggests adding a reference to this assembly.

The missing namespaces are imported according to your namespace import preferences (e.g. import directives are inserted or type names qualifiers are added).
The import directives are added in alphabetical order: all System.* namespaces go first, sorted alphabetically by the second word after dot; all the rest namespaces go next, in alphabetical order.

When you edit a code file, types with missing namespaces are detected with the design-time code inspection (so make sure that it is enabled) and highlighted as errors. By default, the action that imports missing namespaces differs from the normal quick-fix: it is displayed as a pop-up that hovers either over the first unresolved symbol regardless of your caret position:

Namespace import quick-fix
or after a pasted code bock:
Namespace import fix for code block
Thus, you can apply this fix for all unresolved types with the habitual Alt+Enter shortcut without changing your current caret position, so your editing session is not interrupted.

If for some reason you chose not to import a required namespace when the pop-up window was displayed, just press Esc to hide the pop-up. You can also chose not to show this pop-up at all by clearing the Show the "Import namespace" action using popup check box in the ReSharper | Options | Code Inspection | Settings. In these cases, you can import missing namespaces for unresolved types at any time by putting the caret at the non-imported type, pressing Alt+Enter, and choosing the desired quick-fix:

Normal quick-fixes for missing namespace import

To add a namespace import directive on-the-fly

  1. Start typing a name in the editor. If the name references a class that lacks a namespace import directive, the following pop-up window displays:
  2. Press Alt+Enter. If there's a single choice, the namespace will be imported right away. If there are multiple choices, a drop-down list will display where you can select a required type:

This feature is supported in the following languages/technologies:

C# VB.NET C++ HTML ASPX Razor JavaScript TypeScript CSS XML XAML RESX Build Scripts

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

Last modified: 30 June 2015