.net – Resharper Shortcuts for Create Class and Move Class To New File


What are the Resharper 4 shortcuts to

  1. Create a class from usage? e.g. I type "var p = new Person();", and I want to now create the person class.

  2. Move this class to its own file? When the Person class exists in the same file next to my Order class, what is the shortcut to move it.

I can't seem to find these shortcuts on the cheatsheet or the Internet.

Best Solution

Type the line out:

var p = new Person();

Person will be highlighted in red as an error by ReSharper. Put the caret on it and press ALT+ENTER to invoke the quick-fix context menu. Select Create class 'Person'.

The cursor will then be on the new class' name, so press ALT+ENTER again to invoke the context-sensitive quick-fix menu again and select Move to another file to match type name.

That's just two actions - really quick and easy. After a while, it (like most R# commands) becomes muscle memory. Like driving, walking or chewing gum.

The above is all you need to do what you wanted, but you can take it a step or two further:

  1. If you'd rather the class was moved to a different namespace, you can press SHIFT+CTRL+R and select Modify Namespace....

  2. If you'd rather the class was moved to a different project entirely, you can press SHIFT+CTRL+R and select Move to Folder....

The great thing is - ReSharper will make all necessary changes to namespaces to make sure things still compile. With one gotcha - only if the project you move the classes to is referenced by the one you move them from. You have two choices

  1. Go ahead with the refactoring and use ReSharper quick-fixes to both add the reference and import namespaces in one go (if it's a new class, I'd do this because it'll be the only usage).
  2. Add the reference manually before moving them and it'll do it all for you.