ReSharper 2024.3 Help

Adjust Namespaces

This command is a bulk fix that helps you synchronize namespaces with folder structure in any scope, which can be as large as your whole solution. That is, if a namespace in some file does not correspond to the location of the file in the project structure, the namespace is renamed according to the file location and all using directives referencing the modified namespaces are updated.

By default, ReSharper assumes that the namespace each class appears in matches its location in the project. The 'root' namespace for the project is defined in the project properties:

ReSharper code inspection: Namespace does not correspond to file location

As a result of the above, all code elements at the project level are expected to appear in the ShareX namespace, and a warning will be issued otherwise.

Namespaces are made deeper with the introduction of folders. In the example below, a folder called StartupManagers will necessitate all items it contains to appear in the ShareX.StartupManagers namespace. However, there are cases where you do not need the folder to append an extra namespace element. In such cases, you can set Namespace Provider to False in the Project Item Properties popup, which you can invoke by selecting Edit project item properties from the context menu of the folder in the Solution Explorer .

ReSharper: 'Namespace provider' property of a project folder

A folder which is not a Namespace Provider will not influence the namespace of elements it contains, and ReSharper will analyze and refactor the related code accordingly.

Synchronize namespaces with project structure

  1. In the Solution Explorer, select a node where you want to synchronize namespaces.

  2. Do one of the following:

    • Press Control+Shift+R and then choose Adjust Namespaces.

    • Right-click and choose Refactor | Adjust Namespaces from the context menu.

    • Choose ReSharper | Refactor | Adjust Namespaces… from the main menu.

    The Adjust Namespaces dialog will open.

  3. Check the list of files where namespaces do not correspond to the project structure and select files where you want to synchronize namespaces.

    Synchronizing namespaces with project structure
  4. To apply the refactoring, click Next.

  5. If there are no conflicts, the refactoring is performed immediately. If there are types or delegates with same names, which get into the same namespace after the refactoring, the wizard displays a page with conflicts. You can double-click conflicts to navigate to the corresponding declarations and fix them.

ReSharper also provides a code inspection that detects mismatching namespaces. By default, the inspection reports a warning if there is a single namespace declaration in the file and it does not correspond file location in the project structure.

You can synchronize mismatching namespace right from the editor by pressing Alt+Enter and using the corresponding quick-fix: You can also apply the quick-fix in a larger scope, which will give you the same result as performing the refactoring:

ReSharper quick-fix to synchronize namespace to the file location
Last modified: 25 September 2024