Skip to main content

Move Type to File

  • 2 minutes to read

Purpose

Moves a type declaration to a new source code file. The best coding practices require each type to be declared in a separate file. Use this refactoring to isolate the type in a separate source file.

The Move Type to File refactoring can be applied in C# projects that contain file-scoped namespace declarations.

Availability

Available if the caret is in a type declaration and the file contains two or more types. The type must be different from the file name.

Usage

  1. Place the caret in a type declaration.

    public class Person {
        //...
    }
    
  2. Press Ctrl + . or Ctrl + ~ to invoke the Code Actions menu.

  3. Select Move Type to File from the menu.

    move-type-to-file

After execution, this refactoring creates a new source file named after the type, adds this new file to the project, and moves the type to that file.

screencast

The Move Type to File refactoring moves all leading comments, attributes, and XML doc comments to a new file along with the type itself. This refactoring also automatically adds all references used by the type to the new file.

Configuration

Customize Code Action Setting

CodeRush initially opens the generated file when it applies this refactoring. If you want to stay in the source file after a new file is created, perform the following steps:

  1. Open the Editor | C# (Visual Basic) | Code Actions | Code Actions Settings options page and uncheck the “Activate a new file when it is created by refactorings” option.

    code-action-setting

  2. Click Apply and OK to apply this setting and close the options page.

  3. Run the Move Type to File refactoring.

    applied-code-action-setting

For more information, refer to the following topic: Code Actions Settings.

See Also