ReSharper 2016.2 Help

Examples of Context Actions

Change Access Modifier

To change the accessibility of a member or a type, place the caret at the specified access modifier and press Alt+Enter to view the list of available context actions.

Select the desired context action from the drop-down list and press Enter.

Use 'var'

Beginning in C# 3.0 variables can be explicitly or implicitly typed. To change a declaration, place the caret at the type name in the variable declaration and press Alt+Enter, ReSharper offers a context action:

After applying the context action, the variable ownerTag is declared implicitly.

Split Declaration

This declaration statement introduces a new variable ownerTag and assigns a value to it.

Declaration and assignment can be split, so ReSharper offers a context action:

Invert 'if'

Replace auto property with property that uses backing field

Auto properties as other constructs in C# have their own pluses and minuses. If you find that some auto-implemented property doesn't fit your goals, ReSharper can easily replace it with a property that uses a backing field.

Choose and apply the suggested context action.

Remove region/endregion directive

To remove unnecessary #region or #endregion directives, place the caret at a directive or a region name, press Alt+Enter and ReSharper suggests the appropriate context action:

After applying the context action, both directives are removed.

Convert anonymous method to lambda expression

To replace an anonymous method with a lambda expression, place the caret at the delegate keyword and press Alt+Enter to view the list of context actions:

After applying the context action, you get a lambda expression by eliminating the need to rewrite code manually.

Converting a LINQ expression to code

Sometimes it is useful to convert a complex LINQ expression back to code in order to improve code readability.

Place the caret at the query expression and press Alt+Enter to display the list of context actions, then click Convert LINQ to code:
After applying the context action, the query expression is replaced with an appropriate foreach loop.

Iterate via 'foreach'

To iterate through a collection, for example items, type the name of the collection and press Alt+Enter.

Choose the context action and press Enter to apply it. After applying, the foreach statement is generated where you then define the type of identifier, identifier and the embedded statements to be executed.

Use format string

If you want to replace string concatenation with the string.Format method, press Alt+Enter to view the list of context actions.

To replace all arguments of the string concatenation, choose the first context action in the drop-down list. To replace this argument and all its occurrences in the string concatenation, choose the second one.

Check variable for null

If you want to ensure that the variable you work with isn't null, place the caret at the variable and press Alt+Enter and choose Check parameter for null in the drop-down list:

After applying the context action, the corresponding if statement is added:

Move type to another file

If you have more than one type in a single file, ReSharper offers you an opportunity to move one of the types to a separate file:

After applying the context action, the type is moved to another file, that has the same name as the type.

See Also

Last modified: 15 December 2016