Context actions

ReSharper's context actions save you a lot of typing when you need to introduce minor changes to your code. Change access modifiers, generate code that checks for null, convert foreach to for and back — all with a couple of keystrokes. Over 450 context actions are available in C#, VB.NET, XAML, ASP.NET, and other supported languages. Press Alt+Enter over a code element that you want to change, and select a context action to apply.

ReSharper's context actions

Applying context actions

As soon as a context action becomes available for the current caret position, ReSharper displays the hammer icon ReSharper's context action indicator to the left of the caret. If there are several contextually available features, which could be a mix of context actions and quick-fixes, the icon of the action with the highest priority is shown. Click the action indicator or press Alt+Enter to see all available actions.

Similar context actions (for example, actions for different access modifiers) are often grouped together. In this case, expand the group using the right arrow to pick the desired action.

Applying context actions in wider scopes (file, project, or solution)

Applying context actions in wider scopes

Some context actions (Remove #regions, Specify enum member values, Add name to argument, etc.) can be automatically applied in a larger scope: in the current method, class, file, current project or in the whole solution. You can click a small arrow on the right or press the right arrow key on the keyboard to choose where to apply the transformation.

Quick-fixes vs context actions

Context actions vs quick-fixes

ReSharper also provides quick-fixes, which could be displayed in the drop-down list along with context actions. The difference between these is simple: quick-fixes only appear for highlighted code issues aiming to fix them and hopefully improve your code while context actions represent mini-refactorings, which simply change the code.

It is important to note that quick-fixes have higher priority, so if there are any quick-fixes at your caret, you have to press Alt+Enter to see if there are any context actions.

Introducing a null check in C# with a context action

C#: Check parameter for null Example

With ReSharper, you don't have to type anything to check a parameter for null — just press Alt+Enter and ReSharper will generate the whole null-checking routine for you.

Optionally, if you use JetBrains code annotations in your project, the parameter is marked with the [NotNull] attribute so that ReSharper will warn you about any invocations of the method with null for this parameter.

Converting LINQ to code with ReSharper's context action

C#: Convert LINQ to code Example

In this example, you can see how ReSharper's context actions help you quickly convert a LINQ query to a foreach loop and to a lambda expression.

This is the opposite of ReSharper's famous quick-fix that converts loops into LINQ statements.

Converting iterators to collection with ReSharper's context action

C#: Converting iterators to collection Example

Some context actions are more like refactorings — they automate complex code transformations while preserving the logic.

In this example, ReSharper helps you transform an iterator with yield return statements into code that creates, fills up, and returns a collection.

Splitting declaration and assignment with ReSharper's context action

C#: Split declaration and assignment Example

If you need to add a condition to the initialization of an existing local variable, this context action will help you quickly detach the initialization. Even if the variable is implicitly typed, ReSharper will automatically calculate the correct type and use it in the declaration.

Inverting 'if' expression with ReSharper's context action

C#: Invert 'if' statement Example

With this context action, you can quickly swap the branches of an if statement. ReSharper will make sure that the condition, however complex, is properly negated so that the logic stays unchanged.

Converting anonymous method to lambda expression and back with ReSharper's context action

C#: Anonymous method to lambda and back Example

Whatever syntax is used for a delegate (anonymous method or lambda expression), ReSharper's context actions let you change the syntax in a couple of keystrokes, without rewriting the code by hand.

Creating an overload in VB.NET with a context action

VB.NET: Create overload without parameter Example

This context action lets you create a method overload that uses a default value for the selected parameter, and again, all without typing anything.

The action is also available in C# and TypeScript.

ReSharper's context actions in XML-like code

Context actions in XML-like code Example

In XML-like languages, ReSharper's context actions help you streamline routine editing procedures of expanding/collapsing empty tags, and converting attributes to tags and back. These actions are available in XML, XAML, ASP.NET, build scripts, HTML, and Resx files.

Context actions settings

Configuring context actions

If you don't want ReSharper to show a particular context action in the context menu, you can disable it in the ReSharper Options dialog.