ReSharper 2016.3 Help

Examples of Quick-Fixes

Quick-fixes are available in a variety of circumstances, which is too large to be listed. ReSharper provides a total of more than 700 quick-fixes in all supported languages. Here we will show how to use them using a small selection of situations:

Type Mismatch

Whenever the type of an expression cannot be implicitly cast to the type applicable to the expression context, ReSharper detects this error and highlights it in the editor. After positioning the caret at the highlighted error and pressing Alt+Enter, the user can choose a quick-fix from the following list:

/help/img/dotnet/2016.3/typeMismatch.gif

The effects of applying the proposed quick-fixes are illustrated in the table below.

Quick-fix

Effect

Cast to 'string'
/help/img/dotnet/2016.3/typeMismatch1.gif
Safely cast to 'string'
/help/img/dotnet/2016.3/typeMismatch2.gif
Change type of 'o' to 'string'
/help/img/dotnet/2016.3/typeMismatch5.png
Change type of 's' to 'object'
/help/img/dotnet/2016.3/typeMismatch3.gif
Call ToString()
/help/img/dotnet/2016.3/typeMismatch4.gif

Undefined Variable

Whenever ReSharper detects an undefined variable in your code, the error is highlighted. After positioning the caret at the highlighted error and pressing Alt+Enter you will be presented with the following list of quick-fixes:

/help/img/dotnet/2016.3/undefinedVariable.gif

The effects of applying the proposed quick-fixes are illustrated in the table below.

Quick-fix Effect
Create local variable 's' A new local variable s is created proposing you to choose the desired type.
Create parameter 's' in the containing method

A new parameter is added to the method signature:

/help/img/dotnet/2016.3/undefinedVariable3.gif

Create other

Opens a sub-menu where you can choose to create a field or a property in the corresponding class. For example, if you choose to create a field, a new private field s is created in the current class. The following suggestion appears helping the user to choose the correct type of the new field:

/help/img/dotnet/2016.3/undefinedVariable2.gif

Change all 's'

If you choose Change all, the following suggestion displays:

/help/img/dotnet/2016.3/syncEdit.png

Type the new name only once; ReSharper changes all the other occurrences of the undefined variable:

/help/img/dotnet/2016.3/undefinedVariable1.gif

Undefined Method Call

Whenever ReSharper detects a call to an undefined method in your code, the error gets highlighted. After positioning the caret at the highlighted error and pressing Alt+Enter, ReSharper displays the following list of quick-fixes:

/help/img/dotnet/2016.3/Code_Analysis__Examples_of_Quick-Fixes__undefined_method__quick-fixes.png

After applying a quick-fix, the code gets modified as shown in the following table:

Quick-fix

Effect

Create method 'ResolveManagedReference'

This quick-fix declares a new method with the signature derived from the method call, benefiting developers who prefer top-down programming. In our example, ReSharper creates the following declaration, correctly guessing the return type:

/help/img/dotnet/2016.3/Code_Analysis__Examples_of_Quick-Fixes__undefined_method__return_type.png

ReSharper also suggests applicable types and a name for the parameter of the new method:

/help/img/dotnet/2016.3/Code_Analysis__Examples_of_Quick-Fixes__undefined_method__parameter.png

Create other

This sub-menu includes other quick-fixes involving creation of auxiliary code. For example:

Create parameter 'ResolveManagedReference' in containing method and update hierarchy
This quick-fix creates a new parameter in the current method from the unresolved symbol, and introduces this parameter into any base members and inheritors that the method may have.

or:

Create parameter 'ResolveManagedReference' in containing method
This quick-fix creates a new parameter in the current method from the unresolved symbol but doesn't update its base methods or inheritors.

Change all 'ResolveManagedReference' Similarly to the corresponding quick-fix for undefined variables, this quick-fix allows the user to quickly replace all occurrences of the symbol someMethod.

Forgotten Method Return

If a method is expected to return a value but you forgot to provide a return statement, ReSharper warns you about that by highlighting the closing bracket of the troublesome method. After positioning the caret at the highlighted error and pressing Alt+Enter you will be presented with the following quick-fix:

/help/img/dotnet/2016.3/forgottenReturn.gif

Quick-fix

Effect

Add return statement

After the quick-fix is applied, ReSharper adds a return statement returning null.

Make method return void After the quick-fix is applied, ReSharper replaces the initial return type of the method with void.

Missing Using Directives

Whenever you have a type name in your code that cannot be resolved because you forgot to write a corresponding using directive in your file, a small pop-up window suggests you to import the type:

Namespace import quick-fix

Press Alt+Enter, and the appropriate using directive will be inserted. Should there be multiple types with the matching name, you will be asked to choose the one you wish to use (see Importing Namespaces for details).

If for some reason you chose not to import a required namespace when the pop-up window was displayed, or if you cleared the Show the "Import namespace" action using popup check box on the Code Inspection | Settings page of ReSharper options, you can import a type at any time by putting the caret at the non-imported type, pressing Alt+Enter after a quick-fix shows up, and selecting the Import type [name] quick-fix:

/help/img/dotnet/2016.3/Coding_Assistance__Importing_Namespace_02.png

Missing async modifier

If you have a method that contains the await operator, but that is not defined as asynchronous, ReSharper detects such mismatch and offers to fix this problem using the corresponding quick-fix.

Before:

/help/img/dotnet/2016.3/Code_Analysis__Examples_of_Quick-Fixes__make_async_01.png

Applying the quick-fix:

/help/img/dotnet/2016.3/Code_Analysis__Examples_of_Quick-Fixes__make_async_02.png

After:

/help/img/dotnet/2016.3/Code_Analysis__Examples_of_Quick-Fixes__make_async_03.png

Converting a loop to a LINQ expression

With C# 3.0 and LINQ, developers are able to write data-intensive code more easily by directly describing their intent to the compiler. ReSharper detects code that can be rewritten using LINQ syntax and offers to perform the conversion automatically:

Before:

/help/img/dotnet/2016.3/Code_Analysis__Examples_of_Quick-Fixes__loops_to_LINQ_01.png

Applying the quick-fix:

/help/img/dotnet/2016.3/Code_Analysis__Examples_of_Quick-Fixes__loops_to_LINQ_02.png

After:

/help/img/dotnet/2016.3/Code_Analysis__Examples_of_Quick-Fixes__loops_to_LINQ_03.png

Migrating to IEnumerable in method parameters and returns

ReSharper scans your code base to detect methods that can safely return and accept IEnumerable instead of a more specific type such as Array, List, or ArrayList:

Before:

/help/img/dotnet/2016.3/Code_Analysis__Examples_of_Quick-Fixes__use_ienumerable_01.png

Applying the quick-fix:

/help/img/dotnet/2016.3/Code_Analysis__Examples_of_Quick-Fixes__use_ienumerable_02.png

After:

/help/img/dotnet/2016.3/Code_Analysis__Examples_of_Quick-Fixes__use_ienumerable_03.png

Converting assignment statements to object initializers

ReSharper provides both a context action and a quick-fix to convert assignment statements into object initializers. The context action lets you add field assignments to an initializer one-by-one, whereas the quick-fix adds them all in one go. Here's how the quick-fix works:

Before:

/help/img/dotnet/2016.3/Code_Analysis__Examples_of_Quick-Fixes__object_initializers__01.png

Applying the quick-fix:

/help/img/dotnet/2016.3/Code_Analysis__Examples_of_Quick-Fixes__object_initializers__02.png

After:

/help/img/dotnet/2016.3/Code_Analysis__Examples_of_Quick-Fixes__object_initializers__03.png

Converting a static method invocation to extension method call

When you invoke an extension method as a traditional C# static method, ReSharper helps you quickly comply with standard extension method call practices:

Before:

/help/img/dotnet/2016.3/Code_Analysis__Examples_of_Quick-Fixes__invoke_as_extension_method__01.png

Applying the quick-fix:

/help/img/dotnet/2016.3/Code_Analysis__Examples_of_Quick-Fixes__invoke_as_extension_method__02.png

After:

/help/img/dotnet/2016.3/Code_Analysis__Examples_of_Quick-Fixes__invoke_as_extension_method__03.png

Converting an anonymous method to lambda expression

ReSharper suggests converting anonymous methods to lambda expressions. The reverse functionality is provided as a context action.

Before:

/help/img/dotnet/2016.3/Code_Analysis__Examples_of_Quick-Fixes__to_lambda__01.png

Applying the quick-fix:

/help/img/dotnet/2016.3/Code_Analysis__Examples_of_Quick-Fixes__to_lambda__02.png

After:

/help/img/dotnet/2016.3/Code_Analysis__Examples_of_Quick-Fixes__to_lambda__03.png

Converting to auto-property

ReSharper makes it quick to convert traditional, private field-based properties to automatic properties implemented in C# 3.0. For more information, see Using Auto-Properties.

Before:

/help/img/dotnet/2016.3/Code_Analysis__Examples_of_Quick-Fixes__convert_to_auto_property__01.png

Applying the quick-fix:

/help/img/dotnet/2016.3/Code_Analysis__Examples_of_Quick-Fixes__convert_to_auto_property__02.png

After:

/help/img/dotnet/2016.3/Code_Analysis__Examples_of_Quick-Fixes__convert_to_auto_property__03.png

Making type parameter invariant

When a usage of a type parameter violates its variance, ReSharper suggests to make the parameter invariant:

Before:

/help/img/dotnet/2016.3/Code_Analysis__Examples_of_Quick-Fixes__make_invariant__01.png

Applying the quick-fix:

/help/img/dotnet/2016.3/Code_Analysis__Examples_of_Quick-Fixes__make_invariant__02.png

After:

/help/img/dotnet/2016.3/Code_Analysis__Examples_of_Quick-Fixes__make_invariant__03.png

Last modified: 12 October 2017

See Also