Code Cleanup

ReSharper provides Code Cleanup — an extended feature set that combines code formatting with removal of redundant code and applying team coding conventions.

With Code Cleanup, you can not only reformat code but also remove code redundancies, reorder type members, migrate to recent versions of C#, and perform a lot more tasks, all with a single shortcut: Ctrl+Alt+F. That means, you can save a myriad of "go-to-next-highlight-then-apply-a-quick-fix" iterations by simply running Code Cleanup!

Two default profiles — Full Cleanup and Reformat Code — can be complemented by your custom task-specific profiles.

Code Formatting

ReSharper can reformat an arbitrary selected block of code, all code in the current file, all files in a directory, or even in the entire project or solution, according to your Code Style preferences. ReSharper provides distinct formatting options for C#, VB.NET, JavaScript, CSS and XML-like code. Reformatting affects braces layout, blank lines, line wrapping and line breaks, spaces in various contexts, indentation, aligning multiple constructs, and a lot more options that you can fine-tune in the ReSharper Options dialog box.

To reformat the code in the current file, simply press Ctrl+Alt+F, select Reformat Code in Available profiles, and click Run.

Optimizing Namespace Import Directives

To make your code more readable, you can remove redundant using directives from your C# and VB.NET code files. With ReSharper, this tedious process is reduced to a few simple actions. Press Ctrl+Alt+F, click Edit Profiles to create a new Code Cleanup profile, select Optimize 'using' directives (or Optimize 'import' directives for VB.NET) in the list of cleanup options, and apply the profile by clicking Run in the Code Cleanup dialog box:

Removing Code Redundancies (C# only)

After ReSharper has analyzed your code, it knows pretty well where there's an empty constructor, a redundant delegate creation expression, or an 'else' keyword that is not required at all. Overall, ReSharper can remove about 30 redundancies automatically, without asking for your approval every time. To do that, press Ctrl+Alt+F, click Edit Profiles to create a new Code Cleanup profile, select Remove code redundancies in the list of cleanup options, and apply the profile by clicking Run in the Code Cleanup dialog box:

Migrating to Newer Versions of C#

Code Cleanup is a huge helper when you face the task of adopting code constructs introduced in C# 3.0. Specifically, it helps switch to auto-properties, and use 'var' in local variables and iterators according to specific rules. To migrate to C# 3.0, press Ctrl+Alt+F, click Edit Profiles to create a new Code Cleanup profile, select Use auto-property, if possible and configure using 'var' in declarations. Having done that, apply the new profile by clicking Run in the Code Cleanup dialog box:

File and Type Layout (C# only)

In C# code, ReSharper can automatically reorder file and type members in your code. The members will be sorted, grouped, wrapped to specified regions, etc. according to their kind and access modifiers. ReSharper provides two default layout patterns (sets of reordering rules) and a smart visual editor.


Code Style Configuration and Sharing

With hundreds of options, you can fully customize the way ReSharper treats various aspects of your code style when it expands templates, completes and generates code, inserts delimiters, or when you run code cleanup.


All style settings can be easily shared between team members using the distributed storage of ReSharper settings.

Note:

All keyboard shortcuts provided in the "Features" section are taken from ReSharper 2.x / IDEA keyboard scheme. For details on ReSharper's two default keymaps, see Documentation and Demos.