How-To's

ReSharper in Detail: Quick-Fixes vs Context Actions

Of all ReSharper’s features, two are most noticeable: context actions and quick fixes. Along with marker bar, these are exactly what a user sees every time he works with a real-world solution.
 
It’s pretty obvious what these features do – both offer drop-down lists filled with various available actions for modifying your code. However, specific differences between context actions and quick-fixes are not readily evident. Bearing that in mind, we will compare the two features below.
 
Availability of quick-fixes and context actions for a code position is designated by two differently colored bulb icons to the left of current line:

  • red for quick-fixes, and
  • yellow for context actions.

Whenever both quick-fixes and context actions are available at a specific caret position, a red bulb is displayed.
 
When any of two bulbs is displayed, pressing Alt+Enter launches a drop-down list with all available quick-fixes and/or context actions. If only quick-fixes are available, the list looks like this:


 
If only context actions are provided:


 
If both quick-fixes and context actions are available:


 
Quick-fixes are specifically designed to fix any code with highlighed errors, warnings, suggestions, or unused elements. In other words, wherever you see a code item marked with a red, green or blue curly underline, or a symbol highlighted in grey, you can bet there’s a quick-fix provided. Applying a quick-fix at a specific line can cause code modification in any other corner of your solution. For example, the “Create Method” quick-fix applied upon a call to a nonexistent method causes ReSharper to create the corresponding method after the current method ends or in another class:
 
Before applying:

 
After applying:

 
Another example would be triggering a quick-fix over a method call to modify the type of an argument in the corresponding method declaration, which can possibly reside in another file:
 
Before applying:
(Method call)

(Method declaration)

 
After applying:
(Method call)

(Method declaration)

 
On the contrary, context actions serve to provide ways to change your code instantly. They’re not associated with any discovered errors but displayed merely because they might be useful to you. After all, if you’re looking to surround a string with a string.Format() call or merge two nested if statements, it’s much easier to do with context actions than by hand.
 
Since quick-fixes are tied to the results of code analysis, they’re only applied to C# code. As for context actions, three separate sets of them are available for C#, VB.NET and XML.
 
For both C# and VB.NET, you can explicitly choose which context actions to enable. That can be done using language-specific Context Actions tabs in ReSharper Options. No comprehensive list of quick-fixes is available to a user and you can’t disable quick-fixes associated with compilation errors one by one – however, you can abandon code analysis altogether (to do that, clear Enable code analysis in ReSharper | Options | Code Inspection | Settings). You can also fine-tune the list of displayed warnings and suggestions that have quick-fixes associated with them (ReSharper | Options | Code Inspection | Inspection Severity).
 
For examples of using context actions and quick-fixes in practice, see the following links:

Technorati tags: ReSharper, ReSharper 3.1, Resharper in Detail, context actions, quick-fixes, code analysis, VB.NET, C#, XML
image description