Top 10 Resharper Techniques

Resharper is a great tool for improving your productivity. Here are my Top 10 favorite time-savers in Resharper.

#10. Code Reordering

How many times have you cut-and-pasted a property just to move it up or down in a file? If you’re like me and you want to keep your code organized, all that cutting-and-pasting can add up.

Fortunately Resharper provides an awesome feature called “Code Reordering”. Click on a property or method and press Ctrl+Alt+Shift+Up/Down. Resharper will correctly move it up/down in the file, preserving your formatting and comments.

code-reordering-2

 

This also works for moving method parameters left/right, and lines of code up/down.

#9. Import Namespaces

Isn’t it annoying to have to manually add all those using statements to the top of the file? For example, let’s say you’re coding along, and you decide you need an IEnumerable<string> to hold a list of names. So you type the following line and get the infamous red highlighting.

import-namespace-2

 

With Resharper, all you need to do is press Alt+Enter and it will automatically add the appropriate using statement to the top of the file.

#8. Adjust Namespaces

Ideally your namespaces match the your folder structure. But programmers move files around all the time, and inevitably the two will get out of sync. Enter Resharper’s “Adjust Namespaces” tool. Right click a folder in your project and choose Refactor->Adjust Namespaces. Resharper will show you any namespaces that don’t match the folder structure, and give you the option to rename them all with one button.

adjust-namespaces-1

 

#7. Implement Members

Manually implementing interface members on a class is boring. Let’s have Resharper do it! Click the red lightbulb on the left of the red-underlined class definition, then choose Implement Members.

implement-members-2

 

Resharper will let you chose which members to implement. It will automatically create the members to match the base class or interface. Just replace the “NotImplementedException” with your code.

implement-members-3

 

#6. Encapsulate Field

C# is a great language, but creating boilerplate property getters for every class can get very tedious. This is where resharper really shines. Select any field and press Ctrl+R,E. Resharper will offer to encapsulate the field in a property. The result is time saved.

encapsulate-field-1

 

#5. Generate Methods

A lot of our time as programmers is spent writing boilerplate methods. Resharper can help with this. Press Ctrl+Insert from within any class file to access the “Generate” context menu.

Constructors

Many constructors are simple dependency injection methods. They just take parameters and assign them to fields within the class. Resharper can generate a simple constructor like this automatically.

generate-constructor-1

 

Overriding Members

Need to override a method in the base class. Why type the method signature again? Let Resharper do it!

implement-members-3

 

Equality Members

If you want to implement a true value-object, you need to implement the Equals method, GetHashCode, and Equality operators. Fortunately Resharper can do this automatically. For example, consider a simple ID class.

generate-equality-members-1

 

Two SimpleId’s should be Equal if they have the same Value, even if they are different instances of SimpleId. Resharper automatically generated all the code below.

generate-equality-members-2

 

#4. LINQ Suggestions

LINQ is an extremely powerful tool for querying lists and other IEnumerables, but it’s easy to get lost in the complexity. Resharper is a great help in this regard. It actually understands what you are trying to do with the LINQ query and suggests simpler ways to accomplish the same thing.

For example Resharper can simplify this mess

linq-suggestion-1

 

Into this

linq-suggestion-2

 

#3. Copy Type

There are few things more annoying than making a slightly different version of the same class over and over again. Copy, paste, rename, repeat. Resharper comes to the rescue again with the “Copy Type” feature. Right Click a class and choose Refactor->Copy Type. Resharper will ask you for the new class name and namespace.

copy-type-1

 

It will generate a copy of the class and automatically rename the new class, constructor, and any references to the old class within the new class.

copy-type-2

 

#2. Rename

The infamous rename: the crux of all refactoring. Resharper takes Visual Studio’s rename feature to a whole new level. It automatically finds and corrects all references to a symbol.

rename-1

 

It even has an option to rename related local variables, XML references, and comments across the entire solution.

rename-2

 

Renaming is essential to refactoring, and Resharper is good at it.

#1. Code Cleanup

Finally my #1 favorite Resharper feature: Code Cleanup.

What’s worse than opening a file and discovering that the code is poorly formatted? Spending precious development time cleaning it up! Developers can get very bent out of shape when they have to deal with code that isn’t formatted to their liking or to “project standards”. Some projects take a hands-off approach to this problem, letting each programmer use their own formatting style. However this leads to hodge-podge formatting when one programmer works on something that was created by another. In my experience, the solution is to just pick a formatting style and let Resharper handle it for the entire team.

Resharper makes it easy to set up your formatting options for the project.

code-cleanup-1

 

It has tons of options so you can customize the coding style to your liking.

code-cleanup-2

 

You can then check-in the resharper settings file into source control, and you’re ready to go. Next time a team member pulls from the repository, Their Resharper formatting settings will be updated. Now each time a programmer writes new code, they just press Ctrl+E,F and resharper’s Code Cleanup will automatically format the file to team standards. No more bickering! That’s less time wasted on formatting, and more time that can be spent on writing good code.

Conclusion

I recommend Resharper for any teams larger than zero people. It’s a great productivity tool, and once you try it you’ll wonder how you ever lived without it.

You can download a free 30 day trial from jetbrains.com/resharper.

 

One Response to “Top 10 Resharper Techniques”

  1. One of my favorites not mentioned is constructor’s “Introduce and initialize field” and “Initialize field from constructor” – makes otherwise tedious things like encapsulating stuff into data classes or injecting values so much easier.

    Reply

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=""> <s> <strike> <strong>