All docs
V21.1
21.1
20.2
The page you are viewing does not exist in version 20.2. This link will take you to the root page.
20.1
The page you are viewing does not exist in version 20.1. This link will take you to the root page.
19.2
The page you are viewing does not exist in version 19.2. This link will take you to the root page.
19.1
The page you are viewing does not exist in version 19.1. This link will take you to the root page.
18.2
The page you are viewing does not exist in version 18.2. This link will take you to the root page.
18.1
The page you are viewing does not exist in version 18.1. This link will take you to the root page.
17.2
The page you are viewing does not exist in version 17.2. This link will take you to the root page.

Move All Types to Files

  • 4 minutes to read

Purpose

Moves all types or selected types from one file into new separate files. The best coding practices require each type to be declared in a separate file.

Availability

Available when the caret is in a type declaration, and the file contains two or more types.

Usage

  1. Place the caret in a type declaration.

    namespace ConsoleApp
    {
       class A
       {
          //...
       }
    
       class B
       {
          //...
       }
    
       class C
       {
          //...
       }
    }
    
  2. Press the Ctrl + . or Ctrl + ~ shortcut to invoke the Code Actions menu and select Move All Types to Files.

    menu-item

  3. In the “Types to move” window, select the types you want this refactoring to move to separate files. To allow this refactoring to move all types to separate files, leave the “select all” option as it is.

    menu-item

  4. Press Enter to close the “Types to move” window and apply this refactoring.

After execution, the Move All Types to Files refactoring moves the selected types or all types from the source file to new separate files. These types can contain leading comments, attributes, and references in the source file. Each new file has the same name as the type it contains.

screencast

CodeRush can drop a marker into the source file code where you run this refactoring if the Marker feature is enabled.

marker

See the following topic section for more details: Markers: How to Enable.

Note

If your project includes a file whose name matches the type name you want to move to a new file, this refactoring creates a file with a name that contains the type name and number (for example, “TypeName1.cs).

Run “Move All Types to Files” for Folder and Project

You can run the Move All Types to Files refactoring in the Solution Explorer for a selected project, folder, or folders that contain files with different types inside.

Right-click the project, folder, or folders and select the “Move All Types to Separate Files” item.

solution-explorer

Run “Move All Types to Files” in Code Cleanup

CodeRush can automatically invoke this refactoring as a part of code cleanup. Follow the steps below to customize this behavior:

  1. Open the Editor | C# (Visual Basic) | Code Cleanup options page, select the Move types to separate files rule, and enable the Apply in Action option for this rule.

    code-cleanup

  2. To apply this rule in code cleanup before a file is saved, enable the Apply on Save and Apply Code Cleanup when saving a document options for this rule.

  3. Click Apply and OK to save these settings and close the Code Cleanup options page.

  4. Run code cleanup.

    apply-cleanup-rule

    For more information on the Code Cleanup feature, see the following topic: Code Cleanup.

Configuration

You can configure the Move All Types to Files refactoring on the Editor | All Languages | Code Actions | Move All Types to Files options page.

options

For information on the refactoring settings, see the following topic: Move All Types to Files Settings.

Customize Code Action Setting

CodeRush initially opens a generated file when it applies this refactoring. If you want to stay in the source file after new files are 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.

    uncheck-activate-new-file

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

  3. Run the Move All Types to Files refactoring.

    uncheck-activate-new-file

Configure Naming Conventions

You can use the “Generic types’ file naming rule” setting on the Editor | C# | Naming Conventions options page to configure the naming convention for file names of generic types. When you run this refactoring, CodeRush applies this naming convention.

naming-conventions

The default value of this setting is “Type name with apostrophe and parameters count”.

Perform the following actions to change this naming convention setting:

  1. Set the “Generic types’ file naming rule” setting to “Type name with parameters count” as shown in the screenshot above.

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

  3. Copy and paste the following code into your project:

    namespace App
    {
        public class PagedQuery<TOut> : PagedQueryBase, IQuery<TOut>
        {
            //...
        }
    
        class MyClass
        {
            //...
        }    
    }
    
  4. Run the Move All Types to Files refactoring to apply the modified naming convention setting.

    generic-file-name

See Also