Tuesday, November 20, 2001 ..:: Home ::.. Register  Login
   TechTidBits (Blog)  

Renaming a class in Visual Studio? How hard can it be?

Oct 20

Written by:
Saturday, October 20, 2018 2:47 PM  RssIcon

What type of dev are you? If someone else were to look at your code? Would they see one file with 20 classes in there? Or would they see 20 files, each with one class inside? Which type of dev are you? If you don’t know and need a moment, go look at one of your recent projects. Go ahead…I’ll wait…. It’s important.

Well, ok, it’s not THAT important, but it does have a few KEY benefits to having a “one class = one file” mentality. Here are just a couple off the top of my head

  • easy to find classes in solution explorer
  • easy to find methods within a class when classes are small
  • merge conflicts are drastically reduced AND they are focused to truly merge issues and not some phantom problem when you have to manually merge code, this important no matter what type of source control your using (Visual Source Safe, TFS, Git, SVN, PVCS, yes, there are MANY type of source control systems, not just one and they all do merges slightly differently…unfortunately)
  • other members on the team will be easily able to follow your code and likewise, you following theirs (having multiple classes in one file, MIGHT make it easy for one person to follow, but add two or more people to your team, and it’s an ever increasing problem)
  • with multiples and nesting classes, it is easier to get into a situation where you have namespaces matching class names which is conceptually challenging to understand and follow for coding and debugging
  • new teammates coming onboard have a much easier time getting the lay of the land with one class = one file

imageIf you are looking for excuses about having to create a new file…I’m sorry. You’re talking to a Visual Studio keyboard shortcut junkie. I use CTRL+SHIFT_C and you’re now typing out a class name. After you hit enter, you have an internal class (where you can quickly add public in front of class, AND your namespace is already defined (give you are in the right directory). And “seeing all the classes in one place” is just an excuse with me as well, with F12 to jump to definition and CTRL+- (dash) to get back to where you came from, that’s not an issue. There is also Peek (ALT+F12)!

Ok, so what do you do if you’re in the situation where you have multiple classes in one class? Well, first, a few ground rules.

  • one class = one file (yes, yes Peter, we KNOW this already!)
  • name of the class matches the name of the file, change one, change the other
  • CTOR (constructors) will need to be changed as well when you change something
  • anyone calling into these classes will need to be changed as well (like inherited classes)

Quick Interview Pop Quiz Question => What is the default class scope if you omit it? (ie you see something like class MyTestClass{} what is the SCOPE of that class)? Got it? Did you guess or did you KNOW it was internal?! (don’t believe me, check out MSDN ( look under Class and Struct Accessibility).

Ok, so what is the easiest way to get yourself OUT of this problem? Well, there is the BRUTE FORCE method.

  • go into the big file,
  • copy\paste one class (don’t cut just in case something screws up or you get distracted and lose track of stuff, you don’t want to lose your code no matter what you do!)
  • create a new file in the right place (either where you are, or in a new directory)
  • Paste in your code
  • CTRL+S (if you don’t know that one…..hmmmmm ……????)
  • return back to your original file and delete

There is another way, faster and less error prone. The key here to remember is you can use Visual Studio to help you do the refactoring, and yes, there IS a keyboard shortcut for refactoring (CTRL+. (dot, period)). If you look at the screen cap below, you’ll see I right clicked on the Motorcycle class name and I see the “Quick Actions and Refactorings…”. And it’s the first option, fancy that?


I usually just go straight for the CTRL+.


You see the last option of “Move type to Motorcycle.cs”? THAT'S going to do all that other stuff I said above….BUT with the addition of adding in the right namespaces so you don’t have to accidentally get it wrong. It’s beautiful!

Visual Studio does some other things to help you out too. IF you have nested, nested classes, as in this cooked up example.


VS will continue to give you proper options. It’s going to ignore judging your class architecture and let you create your file with fully nested names.


Note: What’s interesting is doing either option, gives me partial Motorcycle classes. Although TECHNICALLY this is correct, having partials around your solution makes editing, finding and debugging difficult. I would strongly recommend against partials unless you REALLY have a good reason for it. And doing THIS is NOT a good reason. If you don’t know what those reasons are yet….don’t worry about it, that means you don’t need it and should get RID of the partials.


Ok, so what’s next to do to the file now? Let’s say I created the Motorcycle.Moped.cs file.


Ok, so now what? Let’s clean up the code. We start with this.


Remove the partial and move to the namespace.


Ok, but we still have the problem of the filename (currently Motorcycle.Moped.cs). Again, the CTRL+. to the rescue (see, it’s a useful refactoring shortcut)


This is the magic hot-sauce you can use to ensure your filename MATCH the class names! And by doing it this way, you don’t have problems with mistyping!

NOTE: the keen and astute readers will notice something here!!!! This is the situation I spoke up above. I renamed the namespace the SAME name as an existing class => BAD!  grrrr These are situations you want to avoid.

Ok, so let’s change the name of the Motorcycle class (I’m going somewhere with this cooked up example, and this is the best way to get there, bear with me). If you open up the Motorcycle.cs class and simply overtype the name, what will happen? Well….

  • you’ll rename the class
  • but have to make sure you rename each and every constructor as well (easy in this example, just one, but what about YOUR code and you have 10, and don’t forget any)
  • now you have to go back to Solution Explorer and rename the file over there

hhmmm seems like a lot of work. YUP! I have an easier way! Hit F2 or right mouse click and Rename.


By doing that, NOW you have the opportunity to type out the name once (no change there really) BUT notice the constructors all change too!


You still need to manually rename the file, again with CTRL+. and picking the file rename option.

Ok, Peter, but one question, Visual Studio lets me rename a file in Solution Explorer. WHY wouldn’t I do THAT? Simply because with that way, you aren’t really renaming the class, you’re simply renaming the file. You will still need to rename the class, then the constructors AND any calling sites to those new renamed methods\constructors.

NOW that you know how to easily rename files, it’s not that hard.

Location: Blogs Parent Separator TechTidBits

Your name:
Gravatar Preview
Your email:
(Optional) Email used only to show Gravatar.
Your website:
Add Comment   Cancel 
Copyright 1999-2012 by   Terms Of Use  Privacy Statement