rename class with file name in one step in Visual Studio

后端 未结 6 1777
醉酒成梦
醉酒成梦 2021-02-18 13:29

I am a long year Java programmer, but currently I code in C#. I am accustomed, that when I change filename, also the class name changes and vice versa. This doesn\'t work in my

相关标签:
6条回答
  • 2021-02-18 13:47

    If you 'refactor' something, the name will be changed everywhere where that text or name has been used so you don't have to follow up changing the name every time its been used. However, you cannot do that with objects beyond the project eg: file name.

    0 讨论(0)
  • 2021-02-18 14:03

    Renaming a file works fine - it'll offer to change the code to match:

    enter image description here

    If you rename the class, then - indeed, this doesn't happen by default. There is no actual need for the two to match, note. But ultimately, rename is only an f2 away.

    0 讨论(0)
  • 2021-02-18 14:03

    As of Visual Studio 2015 Preview 5, the "Quick Actions and Refactorings" context menu contains a "Rename file to [class].cs" command.

    0 讨论(0)
  • 2021-02-18 14:04

    It seems to not work when the copied file is in the same folder and the class name is the same (which it will be if you just copied the file). I think the reasoning may be that rename doesn't descriminate between a class in a newly copied file and the original (See comment below by @xMichal).

    The quick solution is to copy the file (I use ctrl+drag) into a nearby folder, rename, then move it back.

    Another option (which may be Resharper specific), is to rename the class (and constructors) in the copied file, then use the tooltip to electively rename the file to match.

    0 讨论(0)
  • 2021-02-18 14:04

    ReSharper has the MoveTypesIntoMatchingFiles menu option on the Refactor context menu, and it seems to do the trick. Just that if you have more than one class in the same file, they will be moved also into separate files, but that should be a good thing in most cases anyway.

    0 讨论(0)
  • 2021-02-18 14:07

    Install the awesome visual studio plugin called Reshaper. It does that and loads of other nice stuff. Free for 30 days then its costs but is definitely worth it.

    0 讨论(0)
提交回复
热议问题