rename class with file name in one step in Visual Studio

43,890

Solution 1

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.

Solution 2

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

Solution 3

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.

Share:
43,890

Related videos on Youtube

xMichal
Author by

xMichal

Updated on February 01, 2020

Comments

  • xMichal
    xMichal over 4 years

    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 Visual Studio. I must rename the class name and file name separately, and it's sometimes annoying for me, because I always forget on it, so it results to that I have different class and file names. Is there option in Visual Studio to put the renaming in one step?

    • Håkan Fahlstedt
      Håkan Fahlstedt over 10 years
      If you rename the file first, you should get a question wether you want to change the class name too.
    • Admin
      Admin over 10 years
      the filename and the class name are independent - they dont need to match
    • Rui Jarimba
      Rui Jarimba over 10 years
      @mehow: it is a good practice to match the files with the class names
    • Admin
      Admin over 10 years
      but its not necessary. there are languages that require you to keep the name of the file the same name as class but c# is flexible
    • rory.ap
      rory.ap over 6 years
      Come on M$ -- By VS 2017, this should work as expected. If you rename a file and are prompted to rename the associated class, it should work in reverse. VS 2017 has brand new and very cool symbol renaming functionality that lets you rename in situ. It's easy to get carried away and forget that if you do that for classes it won't rename the file too. Drives me nuts.
    • jpaugh
      jpaugh over 6 years
      @rory.ap That exists in VS2015, unless it's improved in 2017. But yes, quite annoying to miss such a small detail.
  • xMichal
    xMichal over 10 years
    now I found, that this works, when you rename the file. But when you rename the class inside of the file, all the references change instead of the file name. And this is my problem.
  • DevOhrion
    DevOhrion over 7 years
    Close, it's Resharper though.
  • mdisibio
    mdisibio about 7 years
    Came across this question because after downloading a sample project created in VS 2015 but opening it in VS 2013, the dialog box you refer to was not coming up when I would rename files. (It comes up as expected when for projects created from scratch in VS 2013) I also got a security warning when opening the downloaded project (which was from a reputable source). Perhaps the symptoms I am describing will help someone else who has the same issue and finds this answer.
  • Marc Gravell
    Marc Gravell almost 7 years
    @LeoNguyen it seems odd that they would remove it in some versions, but it definitely works in VS2017. I'm 80%+ sure that it also worked fine in VS2015, but: I'm not going to reinstall it just to check. Are you sure you don't have an extension installed that is crippling it? (I use vanilla VS)
  • Leo Nguyen
    Leo Nguyen almost 7 years
    I pretty sure it don't prompt anything. I use VS Enterprise 2015 (included Unity Extension)
  • Enzoaeneas
    Enzoaeneas almost 7 years
    @MarcGravell - this is no longer working consistently in vs2017 for some reason. Some files this works, others not and based on my observations, the size of the file does not seem to matter.
  • Justin
    Justin over 6 years
    @Enzoaeneas 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.
  • Justin
    Justin over 6 years
    @MohammedNoureldin - see previous comment. (Sorry; I can only notify one user per comment).
  • rory.ap
    rory.ap over 6 years
    If it works one way (rename file prompts to rename class) why in heck doesn't it work the other way (rename class prompts to rename file). I love the new in situ renaming functionality of VS 2017, and it makes it all that much easier to get carried away and forget that renaming a class doesn't rename the file. I am OCD so it drive's me bananas.
  • Quolonel Questions
    Quolonel Questions almost 6 years
    Does not happen in VS 2017.
  • Raven
    Raven almost 5 years
    This does not happen in VS 2019, its so frustrating I'm 100% sure i had this working in older versions.
  • Simon Mourier
    Simon Mourier over 4 years
    Note for VS 2019, as of today, it still works but not on .NET Standard nor .NET Core projects, documented here: docs.microsoft.com/en-us/visualstudio/ide/reference/… "This refactoring is not yet available for .NET Standard and .NET Core projects."
  • Reyhn
    Reyhn about 3 years
    Unfortunately, this does work in VS2019! )= I checked the "Do not ask again" and pressed "No". Now it always renames everything without asking! And I can't find where to undo the "Do not ask again"-setting! )=
  • Reyhn
    Reyhn about 3 years
    To undo this setting: Tools -> Options -> Projects and Solutions -> General -> Enable/Prompt for symbolic renaming.
  • Chandraprakash
    Chandraprakash almost 3 years
    Works for me consistently in VS2019 Enterprise edition. @Reyhn Thanks for the info!
  • fretje
    fretje over 2 years
    @Reyhn Thanks, I was exactly looking for this info (how to undo this setting)... probably warrants a question on its own ;-)