Skip to main content
A newer version of this page is available. .

Move Type to Namespace

  • 2 minutes to read

Purpose

This Refactoring moves a type declaration to a new namespace and updates all references.

Availability

Available when the caret is on a type declaration and the solution contains two or more namespaces.

Usage

  1. Place the caret on a class declaration.

    NOTE

    The blinking cursor shows the caret's position at which the Refactoring is available.

    namespace NS1 {
        class Class1 {
            // ...
        }
        class Class2 {
            // ...
        }
    }
    
    namespace NS2 {
        class Class3 {
            // ...
        }
    }
    
  2. Use the Ctrl + . or Ctrl + ~ shortcut to invoke the Code Actions Menu.
  3. Select Move Type to File from the menu.
  4. Select a target namespace from the opened menu. If the list of target namespaces is too large, only its part will be displayed. In this case, you can filter the list by typing the desired namespace name while the menu is open. If you need to create a new namespace, type its name and select the Move Type to New Namespace item.

After execution, the Refactoring changes the namespace of the current type and updates all references.

namespace NS1 {
    class Class1 {
        // ...
    }
}

namespace NS2 {
    class Class3 {
        // ...
    }
    class Class2 {
        // ...
    }
}

The namespace change can be done in three ways depending on context.

  • If the current file contains only one namespace, its name will be changed.
  • If the current file contains several namespaces and the target namespace is not in this file, the type will be moved to a separate file.
  • If the current file contains several namespaces and the target namespace is in this file, the type will be moved to the target namespace within this file.