Back to Devexpress

Rename File to Match Type

coderushforroslyn-115571-refactoring-assistance-rename-file-to-match-type.md

latest2.5 KB
Original Source

Rename File to Match Type

  • Aug 16, 2021
  • 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).

  2. Press Ctrl + . or Ctrl + ~ to invoke the Code Actions menu and select Rename File to Match Type.

  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.

csharp
//Filename: Customer.cs
public class Customer {
    private int CustomerID;
    public string FirstName { get; set; }
    public string LastName { get; set; }
}
vb
'Filename: Customer.vb
Public Class Customer
    Private CustomerID As Integer
    Public Property FirstName() As String
    Public Property LastName() As String
End Class

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:

  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:

  4. Run the Rename File to Match Type refactoring to apply the modified naming convention setting.

The screencast below shows the result:

See Also

Rename Type to Match File

Move Type to File