ReSharper, day 19: Implement Members with Alt+Enter

Welcome to day 19 of my 31 Days of ReSharper.

Visual Studio 2003 and 2005 both had features for helping you implement interfaces. VS2003’s features were a lot more finicky. VS2005 was much better. ReSharper is better yet. Let’s take a look.

When you create a new class, and add : IFoo to its declaration, you’ll get a red squiggly, because you now have code that won’t compile (because there are no methods on your class to satisfy that interface). When you put the cursor on the squiggly code, you’ll get a red light bulb, and when you press Alt+Enter, you’ll see the Implement members quick-fix.

ReSharper's quick fix for Implement Members

The same thing happens if you have a non-abstract class, but its parent class has abstract methods. But in this case, there’s a second quick-fix: Make ClassName abstract, which I’ll ignore since it’s pretty self-explanatory.

Note that if your descendant class is already marked abstract, you won’t get a red squiggly for your unimplemented abstract methods, since they aren’t errors. That means you don’t get a quick-fix. ReSharper still has a way to automate these overrides, though, which I’m currently scheduled to cover on Thursday (Day 22).

The Implement/Override Members wizard

The first page of ReSharper's Implement/Override Members wizard, which lets you select which of the missing members to override

The first page of the wizard lets you select which members you want to implement. Everything is selected by default. It would be silly to uncheck anything in the list, because then you’d be left with still-unimplemented interface members or abstract members, so your code still wouldn’t compile.

Note the toolbar buttons in the top right, above the list. These toggle the list between a tree, where everything is grouped by interface, and a flat list, which is sorted alphabetically. This only affects the way they’re displayed in the wizard. ReSharper will still generate the members in any old order it feels like.

There are three options down below: Copy documentation comments, Explicit interface implementations, and Wrap interface implementations in regions. They’re fairly self-explanatory, and I have yet to use any of them, so I won’t cover them further. On to the second page.

The second page of ReSharper's Implement/Override Members wizard, which lets you select which properties you want to generate backing fields for

The second page is where ReSharper’s version becomes much cooler than Visual Studio’s, because you can automatically create backing fields for any or all of your properties. It will add the fields (following the naming conventions you’ve set up in ReSharper Options), and will read and write them from the property getters and setters. Methods, as well as any properties you don’t check in this list, will get placeholder code that throws a NotImplementedException.

If you want to check all of the fields in the list, press Ctrl+A and then Space.

Then you click Finish, and it generates the code for you.

Note: the “Sort alphabetically” checkbox on page 2 is the same setting as those toolbar buttons on page 1: if you check “Sort alphabetically”, you get a flat sorted list, whereas if it’s unchecked, you’ll get the tree. It’s actually just one option for both pages (changing it one place will change it in the other), although they present it differently on the two pages for some unknown reason.

So, there’s Implement Members. Simple but useful. The only option it’s missing is to add a constructor that sets initial values for some or all of those backing fields. There’s an automated way to do that, too (also slated for Day 22), but it’d be nicer if there was an extra wizard page for it. Still, the ability to just implement everything all at once, and create backing fields at the same time, is pretty sweet.

Leave a Reply

Your email address will not be published. Required fields are marked *