Skip to main content

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

  1. Place the caret on a type name (for example, in the Customer class name, as shown below).

    //Filename: Person.cs
    public class Customer {
        private int CustomerID;
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }
    
  2. Press Ctrl + . or Ctrl + ~ to invoke the Code Actions menu and select Rename File to Match Type.

    rename-file-to-match-type-menu

  3. 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.

Style_NamingConventions

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:

  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 Rename File to Match Type refactoring to apply the modified naming convention setting.

The screencast below shows the result:

naming-conventions-setting

See Also