ReSharper 8.0 Web Help

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.

Coding_Assistance__Examples_of_Context_Actions__change_modifier_01

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:

Coding_Assistance__Examples_of_Context_Actions__use_var_01

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

Coding_Assistance__Examples_of_Context_Actions__use_var_02

Tip

This context action is opposite to Specify type explicitly.

Split declaration

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

Coding_Assistance__Examples_of_Context_Actions__split_declaration_01

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

Coding_Assistance__Examples_of_Context_Actions__split_declaration_02

Tip

This context action is opposite to Join declaration and assignment.

Invert if

If you do code restructuring, one of the useful context actions is Invert 'if'.

Coding_Assistance__Examples_of_Context_Actions__invert_if_01

After applying the context action, the condition is replaced with reverse expression and statements are changed correspondingly.

Coding_Assistance__Examples_of_Context_Actions__invert_if_02

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.

Coding_Assistance__Examples_of_Context_Actions__auto_property_to_property_01

Choose and apply the suggested context action.

Coding_Assistance__Examples_of_Context_Actions__auto_property_to_property_02

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:

Coding_Assistance__Examples_of_Context_Actions__remove_region_01

After applying the context action, both directives are removed.

Coding_Assistance__Examples_of_Context_Actions__remove_region_02

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:

Coding_Assistance__Examples_of_Context_Actions__to_lambda_01

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

Coding_Assistance__Examples_of_Context_Actions__to_lambda_02

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.

Coding_Assistance__Examples_of_Context_Actions__LINQ_to_code_01

Place the caret at the query expression and press Alt+Enter to display the list of context actions, then click Convert LINQ to code:

Coding_Assistance__Examples_of_Context_Actions__LINQ_to_code_02

After applying the context action, the query expression is replaced with an appropriate foreach loop.

Coding_Assistance__Examples_of_Context_Actions__LINQ_to_code_03

Iterate via foreach

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

Coding_Assistance__Examples_of_Context_Actions__iterate_via_foreach_01

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.

Coding_Assistance__Examples_of_Context_Actions__iterate_via_foreach_02

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.

Coding_Assistance__Examples_of_Context_Actions__use_format_string_01

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.

Coding_Assistance__Examples_of_Context_Actions__use_format_string_02

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:

Coding_Assistance__Examples_of_Context_Actions__check_for_null_01

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

Coding_Assistance__Examples_of_Context_Actions__check_for_null_02

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:

Coding_Assistance__Examples_of_Context_Actions__move_to_another_file_01

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