JetBrains Rider
 
Get JetBrains Rider
Get your hands on the new features ahead of the release by joining the Early Access Program for Rider 2025.1! Learn more

Adjust Namespaces

Last modified: 25 September 2024

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, JetBrains Rider 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:

JetBrains Rider: Root namespace in the project properties

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 select the folder and open its properties. Then, clear the Namespace Provider checkbox.

JetBrains Rider: '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 JetBrains Rider will analyze and refactor the related code accordingly.

JetBrains Rider 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 AltEnter 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:

JetBrains Rider quick-fix to synchronize namespace to the file location