Context Actions are very similar to Quick-Fixes. The difference between them is the following:
Quick-Fixes let you solve serious problems that lead to compilation errors.
Context Actions are intended for situational code modifications that one is likely to want to perform in a certain codecontext.
These situations (contexts) fall into the following categories:
- Small code inconsistencies (such as excessive parenthesis in an if condition);
- Conformance to coding guidelines (e.g. whether a field or variable declaration and initialization should be on one line);
- Minor code transformations (e.g., convert switch to if-else constructions).
To learn about concrete Context Actions provided by ReSharper, see this List of Context Actions .
Using Context Actions
Context Actions are used in the same way as Quick-Fixes.
To use a Context Action:
- Position the caret in the code editor at a place suitable for a context action. If one or more Context Actions are available for the current code position, a yellow light bulb (looking like this: ) appears to the left of the line. See List of Context Actions below for some suitable contexts.
- Click the yellow light bulb or press Alt + Enter. The list of available actions is displayed.
- Select the desired action to use it.
Sample Context 1
Consider the following
Place the caret on
else will work, too):
Click the yellow light bulb or press Alt + Enter:
Select "Invert if". The
if-else block is inverted as follows:
If we had chosen "Convert to conditional operator,"
then the code would be modified as shown below:
Sample Context 2
Consider the following interface
I and class
C implementing it. Class
C is missing an implementation of
There is a suitable Context Action available for this context:
After it is selected, a default implementation of
Method() is added to class
List of Context Actions (not exhaustive):
- "Use string.Format()" - works on concatentation of two or more strings (where at least one is literal);
- "Split into 2 if's" - works on "&&" in a compound if-operator condition;
- "Merge 2 if's" - currently works on the condition of a nested if-operator;
- Split declaration of multiple variables (locals or fields) into separate declarations;
- Add signature to anonymous method without explicit signature;
- Loop conversion from 'foreach' to 'for' (when iterating over IList or IList);
- Create custom implementation for an event;
- Conversion of switch to if-else ;
- Remove braces in 'if' or other construct when the block consists of only one statement;
- "Invert if" on 'if' keyword;
- To reverse assignment (or multiple assignments in selection);
- Convert 'if' into ?: operator (if possible);
- Convert '== 'into Equals, and vice versa;
- Implement abstract member;
- Merge declarations of a partial type;
- Convert ?: operator to an if-statement;
- Change visibility modifier (available on visibility modifier keywords);
- Split local variable declaration and initialization;
- Join local variable declaration and initialization;
- Convert implicit interface member implementation into explicit and vice versa;
- Navigate implementations (overriding members) of a particular interface (class) in the list of this type's bases.
Quick-Fixes | Advanced Editing Features