24 Resharper Tips
I firmly believe ReSharper is the best thing to happen to .Net since Visual Studio (NCrunch is a close second). In evangelizing it I managed to convince my program to purchase copies for every developer. One caveat: I had to train everyone.
So next week I'm doing a ReSharper presentation at NOVA CodeCamp. To keep the talk interesting I thought it would be fun to incorporate a game of Bingo with ReSharper shortcuts. Get five horizontal, vertical, or diagonal shortcuts when mentioned, yell BINGO, get a prize (Siren of Shame mug anyone? :) ).
Five tips times five tips minus a free spot = 24 tips.
So why not make a blog post while I'm at it? I've been collecting my favorite tips for years now anyway. So without further ado:
1. Navigate to Class (Ctrl+T)
Man I love this shortcut. Need to get to the TrippleDesStringEncryptor quickly? Ctrl+T then type the capital letters "TDSE" and enter.
2. Rename (Ctrl+R+R)
I use this so often it's become muscle memory and I nearly had to look it up. It works differently in different contexts. For local variables it works by renaming all usages inline. For methods there's a quick modal dialog. For classes there's a modal dialog that even allows you to synchronize the file name.
3. Move to Another File: (Alt+Enter)
This is fabulous when you're in quick-and-dirty problem solving mode, throw a new class in the same file, but want to fix it when you switch back to refactor mode. Just put the cursor on the class name and hit Alt+Enter+Enter.
4. Extract variable (Ctrl+R+V)
I use extract variable almost with the frequency of Rename. When you switch back to refactor mode after an intense quick-and-dirty coding session extracting literals or variables is awesome. For instance if you have:
if (!FileInputOutput.Exists(fileName))
You can select everything inside of the if statement, hit Ctrl+R, V, and ReSharper turns it into something lovely and self-documenting like:
var logFileExists = !FileInputOutput.Exists(fileName);
if (logFileExists)
5. Run Unit Tests (Ctrl+U+L)
I've switched to NCrunch for running tests, but if you aren't so lucky Ctrl+U, L is super handy for running all tests in the solution.
6. Rerun Unit Tests (Ctrl+U+U)
This test running alternative is fantastic for when you're working on a single unit test and need to keep running it as you make small changes.
7. Navigate To (F12/Ctrl-Click)
F12 to navigate into a method is nothing new. ReSharper adds the ability to hold the control key and hyperlink into a method with a click of the mouse. Even if you're a keyboard person (like me) this shortcut comes in handy from time to time.
8. Go Back (Ctrl+Minus)
Technically a Visual Studio shortcut, but it's so awesome I just had to list it. I use it constantly if I've just navigated to a method to peek and I want to return back to where I was.
9. Find usages (Shift+F12)
Visual Studio has the ability to find usages of a method, but it pretty much sucks. If a method is called only once, for instance, ReSharper goes right to the callee without popping up any dialogs.
10. Next/Prev Method (Alt+Up/Down)
I tend to navigate more with F12 and Shift+F12, but zipping down the methods a class comes in handy from time to time.
11. Delete line (Shift+Del)
Again a Visual Studio shortcut, but one I use constantly. Yayy to deleting code!
12. Convert to LINQ (Alt+Enter)
So subtle, so mind-blowing. Imagine you have this code:
foreach (var logFile in logFiles)
{
if (!logFiles.Contains("Test"))
{
Console.WriteLine("Found prod file: " + logFile);
}
}
If you place the cursor over the foreach statement ReSharper prompts you "Convert part of body into LINQ-expression". And (with Ctrl+R V) like magic:
var prodLogs = logFiles.Where(logFile => !logFiles.Contains("Test"));
foreach (var logFile in prodLogs)
{
Console.WriteLine("Found prod file: " + logFile);
}
Heck yea! This refactoring works in a huge number of scenarios. It boggles the mind how smart the folks at Jet Brains are to be able to convert procedural code into functional code like that.
13. Convert to Ternary (Alt+Enter)
I have mixed feelings on the ternary operator (e.g. "return hateTernary ? avoidIt : useIt"). These feelings closely mirror my mixed feelings on using multiple return statements from a method (use with caution). To use it on something like this:
public string GetFilePrefix()
{
if (Message.Contains("Test")) return "Test_";
return "Prod_";
}
Put your mouse over the if statement, type Alt+Enter, Enter, combine in an Extract Variable and you get:
public string GetFilePrefix()
{
var isTestLog = Message.Contains("Test");
return isTestLog ? "Test_" : "Prod_";
}
Better, right?
14. Extract Method/ Property (Ctr+R+M)
Visual Studio has something similar, but ReSharper's shows you a little preview and you can uncheck parameters that you'd like to handle yourself. I use this constantly.
15. Insert Code (Alt+Ins)
Useful for inserting constructors and overriding virtual methods. Super-awesome for inserting extremely hard to get correct code like equality members (e.g. you always remember to override GetHashCode and the non-generic overrides and the == and != operators, right?).
16. Make Public/ Private (Alt+Enter)
This shortcut saves typing when you're on public/private/protected keyword, but little known fact: you can also use it on the other end when you're trying to call a method you don't yet have access to.
17. Extract Interface (Ctrl+Shift+R)
I'm not a huge fan of interfaces for reasons I'll enumerate another time, but when you do need them the ability to turn a class into an Interface is really awesome.
18. Navigate to Implementation (Alt+End)
If you're living in code that uses a lot of interfaces you probably hate F12 because it takes you to the interface rather than the implementation. ReSharper and Alt+End to the rescue.
19. Navigate to Base (Alt+Home)
Navigate to Base will take you to an Interface if you're in a base class. It will also take you to the virtual or abstract method you're overriding if you're in a subclass. Very handy.
20. Implement Members (Alt+Enter)
Will make you very happy when you need to override an abstract class or interface. Just put the cursor over the base class or interface and hit Alt+Enter, Enter.
21. Next Warning (Alt+PageDown)
The static code analysis features of ReSharper are legendary. If the tool finds a lot of issue in a given file you can jump through them quickly with Alt+Page Down or Alt+Page Up.
22. Change Signature (Ctrl+R+S)
How often have you wanted to move a parameter from the Nth position in a method to the first or last? If you're anal like me all the time. Ctrl+R S saves the day.
23. Un/Comment (Ctrl+K, C or Ctrl+K, U)
Alright, another Visual Studio shortcut, but I'm always surprised at how few people know about this. Ctrl+K and then C while selecting several lines of code will comment them out. Ctrl+K and then U uncomments.
24. Surround With (Ctrl+E+U)
Such a nice shortcut for surrounding a block of code with an if statement, using block, or try-catch statement.
Summary
I hope you've found these 24 ReSharper tips useful. What are your favorites? Please post in the comments or start a conversation with me on twitter @lprichar.
Bonus Tips
After receiving some great additional tips on twitter and after watching the Resharper Fundamentals pluralsight course I thought I'd add the following three tips:
25. Duplicate Line (Ctrl+D)
Great tip from @g00fygrin, I use this all the time.
26. Locate in Solution Explorer (Shift+Alt+L)
As @therealbennor said this is great when you're jumping around a lot with Ctrl+T and F12. Personally I prefer the "Track Active Item in Solution Explorer" option under "Projects and Solutions", but I know that's not a popular feature.
27. Bookmarks (Ctrl+Shift+1 to set Ctrl+1 to go to)
I learned so much from the Resharper Fundamentals pluralsight course. I can't recommend it enough. This tip in particular made my day. Fantastic for large codebases.