Rename File to Match Type

  • 2 minutes to read

Purpose

Use this refactoring to bring the name of a source code file in line with the type declared in this file. 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, assuming the file name differs from it.

Usage

  1. Place the caret on a type name (for example, class or record).

    NOTE

    The blinking cursor shows the caret's position at which the Refactoring is available.

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

After execution, the 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; }
}
See Also