ReSharper 2016.3 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.

/help/img/dotnet/2016.3/Coding_Assistance__Examples_of_Context_Actions__change_modifier_01.png
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:

/help/img/dotnet/2016.3/Coding_Assistance__Examples_of_Context_Actions__use_var_01.png
After applying the context action, the variable ownerTag is declared implicitly.
/help/img/dotnet/2016.3/Coding_Assistance__Examples_of_Context_Actions__use_var_02.png

Split Declaration

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

/help/img/dotnet/2016.3/Coding_Assistance__Examples_of_Context_Actions__split_declaration_01.png
Declaration and assignment can be split, so ReSharper offers a context action:
/help/img/dotnet/2016.3/Coding_Assistance__Examples_of_Context_Actions__split_declaration_02.png

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.

/help/img/dotnet/2016.3/Coding_Assistance__Examples_of_Context_Actions__auto_property_to_property_01.png
Choose and apply the suggested context action.
/help/img/dotnet/2016.3/Coding_Assistance__Examples_of_Context_Actions__auto_property_to_property_02.png

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:

/help/img/dotnet/2016.3/Coding_Assistance__Examples_of_Context_Actions__remove_region_01.png
After applying the context action, both directives are removed.
/help/img/dotnet/2016.3/Coding_Assistance__Examples_of_Context_Actions__remove_region_02.png

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:

/help/img/dotnet/2016.3/Coding_Assistance__Examples_of_Context_Actions__to_lambda_01.png
After applying the context action, you get a lambda expression by eliminating the need to rewrite code manually.
/help/img/dotnet/2016.3/Coding_Assistance__Examples_of_Context_Actions__to_lambda_02.png

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.

/help/img/dotnet/2016.3/Coding_Assistance__Examples_of_Context_Actions__LINQ_to_code_01.png
Place the caret at the query expression and press Alt+Enter to display the list of context actions, then click Convert LINQ to code:
/help/img/dotnet/2016.3/Coding_Assistance__Examples_of_Context_Actions__LINQ_to_code_02.png
After applying the context action, the query expression is replaced with an appropriate foreach loop.
/help/img/dotnet/2016.3/Coding_Assistance__Examples_of_Context_Actions__LINQ_to_code_03.png

Iterate via 'foreach'

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

/help/img/dotnet/2016.3/Coding_Assistance__Examples_of_Context_Actions__iterate_via_foreach_01.png
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.
/help/img/dotnet/2016.3/Coding_Assistance__Examples_of_Context_Actions__iterate_via_foreach_02.png

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.

/help/img/dotnet/2016.3/Coding_Assistance__Examples_of_Context_Actions__use_format_string_01.png
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.
/help/img/dotnet/2016.3/Coding_Assistance__Examples_of_Context_Actions__use_format_string_02.png

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:

/help/img/dotnet/2016.3/Coding_Assistance__Examples_of_Context_Actions__check_for_null_01.png
After applying the context action, the corresponding if statement is added:
/help/img/dotnet/2016.3/Coding_Assistance__Examples_of_Context_Actions__check_for_null_02.png

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:

/help/img/dotnet/2016.3/Coding_Assistance__Examples_of_Context_Actions__move_to_another_file_01.png
After applying the context action, the type is moved to another file, that has the same name as the type.

See Also

Last modified: 3 April 2017