Click here to Skip to main content
15,885,944 members
Articles / ReSharper

The Wonderful World of ReSharper

Rate me:
Please Sign up or sign in to vote.
4.85/5 (4 votes)
7 Jan 2016CPOL7 min read 10.4K   5   2
I've been using ReSharper for the last 4 years and consider it to be the most useful tool I have yet come across. The purpose of this article is to highlight some of my favourite features.

I've been using ReSharper for the last 4 years and consider it to be the most useful tool I have yet come across. The purpose of this article is to highlight some of my favourite features. For those who are new to ReSharper (where have you been?) this will give you numerous time saving tips and seasoned users may find the odd extra gem tucked away. Feel free to add your favourite shortcuts if I haven't listed them here!

Duplicate Text (CTRL-D)

A very useful time saving feature when performing a lot of text editing. Simply place the cursor anywhere on a line of code to duplicate the entire line below, or highlight to duplicate whatever is highlighted. It’s another way of copying text that doesn’t make use of the Windows clipboard, meaning that you can copy a piece of text (e.g. a property name) from somewhere, use CTRL-D to duplicate a line of code and paste the clipboard text on to the duplicated line where you need it.

Go To Type (CTRL-T)

Quick way to navigate to another type. You can use a further shortcut here by simply typing the first letter of each word in the class name (e.g. to navigate to HomeController, type HC)

Go To File (SHIFT-CTRL-T)

Similar to Go To Type, except that it navigates filenames. This is particularly useful if you are doing BDD (which I’ll cover in another article!) and have multiple classes within the same code file, or are using partial classes.

Go To Symbol (Shift-ALT-T)

Similar to Go To Type and Go To File, this one allows you to search for individual method names. It uses wildcard mappings and first letter shortcut as per Type and File. Massive kudos to SO user @dmitry-osinovskiy for this one!

Go To Definition (CTRL-Click)

If you hold CTRL down whilst moving your mouse over class names or members, they will underline. Clicking will take you to the definition.

File Structure Window (CTRL-ALT-F)

This is a really useful window for analysing and navigating around a class. I tend to stick it on the left where the toolbox lives and it enables you to move code section around within a class very easily, as well as encasing blocks of code within regions. It’s a quick way to give your classes some structure, grouping methods by access modifier, keeping properties and fields separate, etc. If you use FxCop or StyleCop it can be very handy.

Unit Test Runner

The ubiquitous unit test runner. A fine addition to Visual Studio, as anyone who has tried to use Visual Studio’s built in test runner will tell you. Allows you to easily run and debug individual tests or entire test suites from within the Visual Studio IDE, giving a clear indication of passes and failures.

Highlight Usages (SHIFT-ALT-F11)

A variation on this theme has been implemented in Visual Studio 2010, in which occurrences of a class member are highlighted if you click on one. The downside of this approach is that it only works whilst the cursor remains on the member in question. The ReSharper version highlights the member in pink if it is being written to or declared, and blue if it is being read from. It will also tell you if the usage you’re highlighting is the only one. Just hit escape to remove the highlights.

Navigate To (ALT-`)

Great little context menu. Navigate to usages of a symbol, declaration, implementation, the object browser, metadata or related files.

Colour Identifiers

Not so much of a shortcut as a useful code readability feature. If you switch on colour identifiers, properties, methods and fields are coloured purple, which makes the code easier to read. Also, constant values are made bold and hovering over them with your mouse pops the value up in a tooltip. Switch this on via the ReSharper Options menu, under Code Inspection > Settings

Code Cleanup (CTRL-E, F)

While we’re on the subject of the ReSharper menu, under the Tools section at the bottom you’ll find Code Cleanup. Ever worked in an organisation where you wished they’d had coding standards that were followed by everybody? If you’re lucky enough to have coding standards at your organisation, you’re probably already using this feature. Add a new profile and lots of juicy configuration options will be available to make your code even more elegant. Once you’re happy with the options you’ve set, following the company coding standards should be a breeze.

The rather innocuous Reformat code option is the one that does the most damage. Everything that you’ve configured under the Languages formatting section is applied if you have this box checked. Braces, spacing, line breaks, all that stuff. Incidentally, if you don’t like any of the keyboard shortcuts, you can change them via Visual Studio > Tools > Options > Keyboard

Rename (CTRL-R, R)

If you’ve ever had to rename a type using Visual Studio, you’ll be aware of how annoying this can be! It feels far safer with ReSharper. It takes care of changing the filename as part of the rename and updates all references to this type within your codebase. It’s also a quick and safe way of renaming private variables within a class, and even makes intuitive suggestions.

Skip Analysis (CTRL-ALT-SHIFT-8)

ReSharper’s code analysis is a wonderful tool if you’re working on relatively small files, but if you’ve ever worked on a codebase that contains a couple of monster files (we’ve all been there), or you’re looking at a generated file (from a WCF/RIA service generation or something like that) it can be really handy to switch analysis off on this file, particularly if your system is starting to chug a little when it opens certain files. You can do this via the shortcut key combination when you have the file open in Visual Studio. Just look for the coloured analysis summary square just to the right of the code window, next to the scrollbar. It’ll be one of the traffic light colours when analysis is on, but goes grey once you’ve switched it off. You can configure multiple files to be skipped via the ReSharper menu: ReSharper > Options > Code Inspection Settings > Edit Items To Skip

Templates

The last thing I wanted to cover that I’ve found to be invaluable when unit testing, is ReSharper’s support for creating templates. Rather than creating a class each time and manually adding SetUp/TearDown and the various dependencies that your test fixture requires, you can have ReSharper generate one for you, based on a User Defined template. In the example below, we’re going to set up a basic unit test template for NUnit, nothing fancy.

Access the Templates Explorer via ReSharper > Live Templates and select the File Templates tab. Under User Templates, click the icon to add a new template. We’ll make this be a c# template, so click the Everywhere link and select the “in C# projects (including ASP.NET C# projects)” option. You should see the default file name change to Class.cs. Let’s rename that to Test.cs. It’s just a default name but helps remind us that we’re creating a unit test. Then, paste code below into the main editor window. You’ll need to define a couple of macros for this to work, which we’ll look at in turn. Here’s the code:

namespace $namespace$
{
  using NUnit.Framework;

  public class $classname$
  {
    #region SetUp/TearDown

    [SetUp]
    public void SetUp()
    {
    }

    [TearDown]
    public void TearDown()
    {
    }

    #endregion

    public void $FirstTest$()
    {
    }
  }
}

The values between the $ symbols are the macros that we’ll need to define for our template to work properly. Once you copy the code into the left hand pane of the template editor, you should see your 3 macros appear in the right-hand pane. You’ll need to change the 3 macros to the following settings:

  • namespace should use the macro “Default namespace for current file” and should NOT be editable
  • classname should use the macro “Current file name without extension” and should NOT be editable
  • FirstTest should use the macro “Constant value” - set this to whatever value you like. FirstTest will do. It should be editable.

Once you’ve saved your new template, you can test it out (no pun intended). Right-click on your unit test project in Solution Explorer and choose Add > New From Template > More... Find your new Unit Test template from the User Templates section. Select it and check the Add to quicklist option. When you click OK, your Unit Test template should in future appear in the New From Template list.

That’s just a few of the features that I use most often with ReSharper. Feel free to add your favourite ones that I’ve missed!

View original article

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead Levelnis Ltd
United Kingdom United Kingdom
Follow along my journey as I create a newsletter about launching websites. Just message me with "I'm in" and I'll add you

Comments and Discussions

 
Generalthing I love about Resharper Pin
John Torjo7-Jan-16 20:56
professionalJohn Torjo7-Jan-16 20:56 
GeneralMy vote of 5 Pin
Klaus Luedenscheidt7-Jan-16 19:19
Klaus Luedenscheidt7-Jan-16 19:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.