My new favourite Resharper features

I've always known Resharper could improve my productivity but I've just not found the time to invest using it properly yet. I blogged recently about Explore Stack Trace and I'm starting to think of Resharper as an essential piece of kit rather than a nice to have.

However, today I was lucky enough to have my colleague Rupert Benbrook looking over my shoulder whilst I was working on some Prism samples and he's something of a Resharper Ninja and mentored me through some cool features. Here are my favourites so far.

Alt+Insert

I didn't know about this little chap. Hit Alt+Insert and you'll get the Generate menu.

Generate Menu

This is really handy for creating Constructors and the like. I particularly like the fact that if I choose Constructor I get to choose which class members should be specified on the constructor.

Generate Constructor

public class MyClass
{
private readonly string _foo;
public MyClass(string foo)
    {
        _foo = foo;
    }
}
Nice.

Introduce Field and Initialise

Given Prism's (default but pluggable) use of Unity it's really common to need to add a new parameter to a constructor and store the instance as an instance member. It couldn't be easier, just specify the constructor parameter and put the cursor on the new parameter. Now hit Alt+Enter and choose

Introduce Field and Initialise

public class MyClass
{
private readonly string _foo;
private readonly IRegionManager _regionManager;
public MyClass(string foo, IRegionManager regionManager)
    {
        _foo = foo;
        _regionManager = regionManager;
    }
}
Nice!

Initialize Field from Constructor(s) Parameter

And you can go the other way too - have a field that you'd like to initialize in a constructor?

Initialize Field from Constructor(s) Parameter

public class MyClass
{
private readonly string _foo;
private readonly IRegionManager _regionManager;
private readonly IUnityContainer _unityContainer;
public MyClass(string foo, IRegionManager regionManager, IUnityContainer unityContainer)
    {
        _foo = foo;
        _unityContainer = unityContainer;
        _regionManager = regionManager;
    }
}
Niiiice!

Surround With ...

It's common to want to quickly surround a section of code with a try/catch block and I hate doing this manually. With Resharper just hit Ctrl+E followed by U and you'll get the 'Surround With' menu.

Surround With...

public MyClass(string foo, IRegionManager regionManager, IUnityContainer unityContainer)
{
try
    {
        _foo = foo;
        _unityContainer = unityContainer;
        _regionManager = regionManager;
    }
catch (Exception exc)
    {
        Console.WriteLine(exc);
    }
}
Handy.

I'm going to be using Resharper more and more from now on. Let's hope Rupert runs a nice series of Resharper tips on his blog. I'll certainly be watching. Cheers Rupert!

Originally posted by Josh Twist on the 5th of December 2008 here.