Performance Zone is brought to you in partnership with:

.NET developer/architect. Been coding since 1996. Jonas is a DZone MVB and is not an employee of DZone and has posted 14 posts at DZone. You can read more from them at their website. View Full User Profile

How Resharper rocks my average work day.

02.11.2013
| 3760 views |
  • submit to reddit
I’ve been using Resharper since I started coding in .NET/C#. It’s a tool with a lot of features which aids you in the average day development. In this blog post I’m going to show you the features that I use the most.

Enter ALT+Enter

ALT+Enter is the hotkey that you have to start with. It’s a context sensitive beast that will do the most stuff for you.

Working with classes

Let’s say that I create a new class:

public class UserRepository
{
}

In it, I need to use a DbContext. So I create a new member field:
public class UserRepository
{
    DbContext _dbContext;
}

however, when I type the semicolon resharper inserts private for me:

public class UserRepository
{
    private DbContext _dbContext;
}

Resharper sees that it has not been initialized and gives a warning (hoover over the field):

newclass1

Pressing ALT+ENTER creates a constructor for me:

newclass2

which results in:

class UserRepository
{
    private DbContext _dbContext;
 
    public UserRepository(DbContext dbContext)
    {
        if (dbContext == null) throw new ArgumentNullException("dbContext");
        _dbContext = dbContext;
    }
}

now I want to make sure that null is not passed in, so I press ALT+ENTER on the argument name:

newclass3

which adds an argument check:

class UserRepository
{
    private DbContext _dbContext;
 
    public UserRepository(DbContext dbContext)
    {
        if (dbContext == null) throw new ArgumentNullException("dbContext");
        _dbContext = dbContext;
    }
}

Let’s say that I create a new method in which I decide to change the return type:

class UserRepository
{
    private DbContext _dbContext;
 
    public UserRepository(DbContext dbContext)
    {
        if (dbContext == null) throw new ArgumentNullException("dbContext");
        _dbContext = dbContext;
    }
 
    public User[] Find()
    {
        // this is a method with some logic in it
 
        return _dbContext.Users.ToList();
    }
}

But since that doesn’t match the code will not compile. So I press ALT+ENTER on the return statement:

newclass4

Now I want to cache the users. So I need to reference the cache which exists in another project. I do that by writing the name and use ALT+ENTER:

newclass5

That step will add a reference to the other project and import the correct namespace. After that I press ALT+ENTER again on the field name and add a constructor.

Renaming files

Let’s say that I’ve renamed a class by just typing a new name (the file name is still the old one):

rename file

I then press ALT+Enter on the class name:

renamefile2

the file is now renamed.

Update namespaces

The file in the previous example should also be moved to a new folder. So I just drag it to the new one folder. However, the namespace is not correct now, so Resharper gives a warning (by a visual indication = the blue underline):

namespace1

which means that I can just press ALT+ENTER on it to rename the namespace (which also updates all usages in the solution).

namespace2

Summary

ALT+ENTER alone saves you a lot of typing and clicking. But most important: You get a much better flow when coding. No annoying stops for basic bootstrapping.

Unit tests

Resharper gives you a new unit test browser and context actions for each test.

Let’s say that you create a simple test:

tests1

which you run:

tests2

and get a result

test3

The call stack is clickable which makes it easier to browse through the call stack to get an understanding of why the test failed. Anything written to the console (Console.WriteLine()) also gets included in the output.

Code navigation

The code completion in Resharper allows you to use abbreviations (the camel humps) to complete text:

navigation1

If I type one more letter I only get one match

navigation2

When you code against abstractions (interfaces) you’ll probably want to go to the implementation. ReSharper adds a new context menu item which allows you to do so:

navigation3

If there are only one implementation it’s opened directly. If there are more than one you get a list:

navigation4

CTRL+T opens up a dialog which you can use to navigate to any type. You can either type abbreviations or partial names:

navigation5

CTRL+SHIFT+T is similar, but is used for files:

navigation6

Finally we have “Find usages” which analyses the code and finds where a type/method is used

navigation7

navigation8

Code cleanup

This well structured file can be only better:

cleanup1

I press ALT+E, C to initiate the cleanup dialog:

cleanup0

The “usevar” is my own custom rule where I change all possible usages to use var over explicit declarations.

Result:

cleanup2

Notice that the property got moved up above the method and that unused “using” directives where removed. You can also automatically sort methods and properties.

Finally Resharper shows a warning about the property (since it’s not initialized). ALT+Enter and choose to initialize it using a constructor solves that:

cleanup3

Summary

Those are the features that I use most. What are your favorite feature?

Published at DZone with permission of Jonas Gauffin, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Ryan Mcdougall replied on Tue, 2013/02/12 - 10:55am

Do you find yourself using the static analysis feature as well?

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.