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
Place the caret in a type declaration.
Press Ctrl + . or Ctrl + ~ to invoke the Code Actions menu.
Select Move Type to File from the menu.
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.
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:
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.
Click Apply and OK to apply this setting and close the options page.
Run the Move Type to File refactoring.
For more information, refer to the following topic: Code Actions Settings.