Automatically rename class to match file name

6.6k Views Asked by At

I have a class A in file B.cs. Now I want to rename this class to match the file name (B).

Is there a refactoring or context action in VS or ReSharper which automatically can do this renaming for me without typing in the new name matching file name?

Example

Before renaming:

File B.cs:

class A {}

After automatically renaming:

File B.cs:

class B{}

Update

The opposite (automatically rename file to match class name) is not an option because all these file renamings are implemented as DELETE+ADD. But then I would lose change history of that file in Perforce because all VS-Plug-ins for VS-Perforce-connection do not support move/add+move/delete actions for renaming.

4

There are 4 best solutions below

0
On BEST ANSWER

We recently did a large refactor of several files, changing them from "Controllers" to "Services". After plodding through a few of them with ReSharper, I decided I should rename "BaseController" to "BaseService". Ding! ReSharper offered to rename all subclasses and replace "Controller" with "Service".

11
On

In Visual Studio, you can right click on the class name inside the .cs file and rename it by choosing "Refactor --> Rename". It will give you the option to refactor it.

7
On

With Resharper you can use Ctrl+R,O

enter image description here

P.S. I use Resharper 6.1

5
On

Is there a refactoring or context action in VS or ReSharper which automatically can do this renaming for me without typing in the new name matching file name?

With ReShaper 8.1 it is possible. Just put a cursor on the class name, hit alt + Enter to bring up the options then Select 'Rename file to match type name' and you are good.

enter image description here