Joe White’s Blog

Life, .NET, and Cats


ReSharper, day 16: Importing namespaces with Alt+Enter

Welcome to day 16 of my 31 Days of ReSharper.

Today, and for the next few days, I’ll talk about one of the keystrokes you’ll use most often with ReSharper: Alt+Enter. I referred to it earlier as ReSharper’s “Swiss Army knife”, because it’s so versatile: it can fix certain compiler errors, complete code, remove code that’s never used, and just in general automate some of the repetitive tasks that come with writing code.

Today’s topic: importing namespaces.

The basics

As I previously mentioned, ReSharper shows unrecognized symbols in red. But if a symbol is known to exist in some other namespace that you’re not yet using, ReSharper will show something else, too: a hint prompting you to fix the error.

ReSharper prompting you to press Alt+Enter to import a namespace

So if you just type PixelOffsetMode into your source file (and you’re not already using System.Drawing.Drawing2D), ReSharper will scan all the namespaces, find a class with that name, and ask you if it’s got the right one. If you confirm by pressing Alt+Enter, it will add using System.Drawing.Drawing2D to your source file.

Here’s more info worth knowing about this feature:

  • Sorting. ReSharper will add usings according to a predictable ordering rule. It’s a modified alphabetical sort: it puts all the System namespaces first (in alphabetical order), followed by everything else (in alphabetical order). Note that this will only sort of work if they aren’t already ordered this way, but you can just do Ctrl+Alt+F to re-sort them.

  • Less position-dependent. I’ll be talking about other things you can do with Alt+Enter, and usually, they’re specific to the code under the cursor. But this particular usage — adding namespace usings — doesn’t have that requirement: it will work even if the cursor isn’t over the red symbol. (If there are multiple red symbols in the file, the one that Alt+Enter will target is whichever one is closest to the cursor position.)

  • Not (quite) instant. As with the color-coding, you may sometimes have to wait a second or two after you stop typing, before you can see the hint and hit Alt+Enter. ReSharper has to look at your code and realize that (a) it doesn’t recognize this type and (b) the type does exist in another namespace. It only shows the Alt+Enter hint after it’s figured this out, and until it does, Alt+Enter won’t do a thing. It doesn’t take long, but occasionally you’ll have to pause to let it do its thing.

  • Disambiguation. If the symbol is found in more than one namespace, ReSharper will ask you which one you mean. The hint will say “(multiple choices)”, and when you hit Alt+Enter, you’ll get to pick the one you want from a list.

    ReSharper prompting you to press Alt+Enter to import one of multiple interfaces
    ReSharper's selection menu after pressing Alt+Enter

  • Depends on references. This shouldn’t be a surprise. If you create a new assembly and add a [TestFixture] attribute to a class, ReSharper will show it in red, but won’t be able to help you fix it until you right-click on your assembly and Add Reference to NUnit.Framework.dll.

Comparison to Ctrl+Alt+Space

Yesterday, I talked about using Ctrl+Alt+Space to autocomplete type names and add using directives.

Alt+Enter will work in circumstances where Ctrl+Alt+Space won’t, e.g. when you’re pasting code that you got from some code sample online, and it doesn’t have all the usings you need. But I think Ctrl+Alt+Space is a probably better feature for general coding. Mind you, I hardly ever remember to use it; it’ll take a while to get used to thinking, “Okay, I’m about to type a type name, not a method name. So I need to hit Ctrl+Alt+Space this time.” But if I did remember to use it, it feels like a better fit than typing something and then hitting Alt+Enter.

The main reason is that, even though both features have a potential to make you wait a second or two, Ctrl+Alt+Space puts that delay at a more opportune time.

With Alt+Enter, you type an identifier and hit Space, and your mind is already moving along to the next thing you have to type. And then suddenly you see your code turn red, and you have to divert your attention back to what you already wrote. Even if it’s just to decide in the back of your mind, “Oh, I’ll just finish typing this statement and then hit Alt+Enter to fix that,” it’s still a distraction.

Whereas with Ctrl+Alt+Space, you type part of an identifier and then hit the keystroke, and it’s all part of typing that identifier. Then you let your brain move on to the next thing in the line. So your flow isn’t interrupted by having to look back at code you already wrote.

Your mileage may vary (and mine may, too). I’m certainly not saying that Alt+Enter is a bad feature! Just consider using Ctrl+Alt+Space when it fits.

Comparison to Visual Studio’s Alt+Shift+F10

Visual Studio 2005 has a similar feature for importing namespaces: when you type a symbol it knows about, it will show a little smart tag, which you can mouse over (or hit Alt+Shift+F10) to show a dropdown menu with the ability to add a using. How does it compare to ReSharper’s Alt+Enter?

  • Keystrokes. If the symbol only exists in one namespace, ReSharper only requires you to press Alt+Enter. Visual Studio requires the more-unwieldy Alt+Shift+F10, followed by Enter. Advantage: ReSharper.

  • Sorting. Visual Studio adds the namespace to the end of the block of usings, leaving your usings in a haphazard order. ReSharper automatically keeps them sorted. Advantage: ReSharper.

  • Positionality. Visual Studio will only show the Smart Tag when the cursor is on the symbol. ReSharper shows its “add namespace” Alt+Enter hint anytime there’s a red symbol scrolled onto the screen, so you don’t have to cursor around to import namespaces. Advantage: ReSharper.

  • Response time. Visual Studio pops up its smart tag instantly after you type the last letter in the type name. ReSharper can take a second or two before it highlights the symbol in red and shows the hint. Advantage: Visual Studio, by a small margin. (This is much more noticeable on my 800MHz/256MB laptop than it is on our 3GHz/2GB development machines.)

There are no responses to “ReSharper, day 16: Importing namespaces with Alt+Enter” yet.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>


Joe White's Blog copyright © 2004-2011. Portions of the site layout use Yahoo! YUI Reset, Fonts, and Grids.
Proudly powered by WordPress. Entries (RSS) and Comments (RSS). Privacy policy