Rename File to Match Type
- 2 minutes to read
Purpose
Renames a file to match the type name declared in it and updates the project.
The best coding practices require each type to be declared in a separate file named after the type.
Availability
Available when the cursor is in a type name. The file name must differ from the type name.
Usage
Place the caret on a type name (for example, in the Customer class name, as shown below).
Press Ctrl + . or Ctrl + ~ to invoke the Code Actions menu and select Rename File to Match Type.
Press Enter to apply the selected refactoring.
After execution, this refactoring removes the source code file from the project, renames the file and adds the new file to the project.
//Filename: Customer.cs
public class Customer {
private int CustomerID;
public string FirstName { get; set; }
public string LastName { get; set; }
}
Customization
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.
The default value of this setting is “Type name with apostrophe and parameters count”.
For example, perform the following actions to change this naming convention setting:
Set the “Generic types’ file naming rule” setting to “Type name with parameters count” as shown in the screenshot above.
Click OK to apply the setting and close the options page.
Copy and paste the following code into your project:
namespace App { public class PagedQuery<TOut> : PagedQueryBase, IQuery<TOut> { //... } class MyClass { //... } }
Run the Rename File to Match Type refactoring to apply the modified naming convention setting.
The screencast below shows the result: