Quick-fixes

Applying quick-fixes
Quick-fixes are most often represented by the red
(for errors) or yellow
(for warnings, suggestions and hints) light bulb
that appears to the left of a highlighted code line when you set the caret on the
highlight.
You can see the list of available quick-fixes for a given error either by clicking the
light bulb or by pressing Alt+Enter. Then simply select an appropriate quick-fix
from the list, and the problem will be resolved in the most convenient way.

Fix in scope
Some quick-fixes (e.g. Remove unused directives, Make field read-only, Remove redundant cast, etc.) can automatically find and fix issues in a larger scope: in the current file, in the current project or in the whole solution.
You can recognize such fixes by a small arrow displayed next to them. Click this arrow or press the right arrow key on the keyboard to choose where to apply the fix.

Fixing unresolved symbols
For errors caused by unresolved symbols, ReSharper provides a number of different quick-fixes, for example:
- If a symbol exists in some namespace referenced anywhere in your solution, ReSharper shows a pop-up suggesting to import all missing namespaces in the file.
- A set of Create... quick-fixes will help you quickly generate various declarations of the symbol according to the usage context.
- Another way to fix the problem is Find this type on nuget.org.... It will bring up the NuGet Browser, which will help you find and install a missing NuGet package.

Interactive quick-fixes
Quick-fixes that create new symbols in your code are often made interactive,
like in this example where ReSharper helps you fix multiple enumerations of
IEnumerable
by forcing the enumeration in advance to a List<>
variable.
When ReSharper highlights required input fields with a red frame, you can accept the suggested value or modify it and then press Tab or Enter to move to the next input position, or press Shift+Tab to move to the previous input position. Once you've done with the last input field, your caret returns to the normal mode.

Quick-fixes vs context actions
ReSharper also provides context actions that are displayed in the drop-down list along with quick-fixes.
The difference between these is simple: quick-fixes only appear for highlighted code issues aiming to fix them while context actions represent mini-refactorings that are always available in the Alt+Enter drop-down.

Your custom quick-fixes
ReSharper provides Structural Search and Replace to find code that matches a certain pattern, and optionally replace it with code matching another pattern. Even more exciting is that ReSharper can continuously monitor your solution for your search patterns, highlight code that matches them, and provide quick-fixes to replace the code according to your replace patterns.

C#: Use string interpolation Example
For years, using String.Format
and other methods that support composite formatting was
the only way to embed C# variable values into string literals.
Starting with C# 6.0, you can do this in a much more readable fashion using string interpolation.
With ReSharper's quick-fix, replacing composite formatting with string interpolation will only take a couple of keystrokes.

C#: Fix possible NullReferenceException Example
When ReSharper detects a method call on an object that could be null, possibly leading to a
System.NullReferenceException
at runtime, it suggests two quick-fixes.
A traditional fix will add a null checking routine ahead of the call.
However, a more succinct way of dealing with this call is to use the null-conditional .?
operator, which was introduced in C# 6.0
to address this scenario.

C#: Convert foreach into LINQ expression Example
If you like LINQ syntax in C#, ReSharper will help you check your existing codebases for loops that could be converted into LINQ expressions and then you can use a quick-fix to perform the conversion quickly and safely.

C#: Remove redundant array creation Example
If a C# method takes a variable number of arguments with the params
keyword,
an array for the arguments will be generated by the compiler, so you don't have to create an array for your arguments in the method call.
What's more interesting here is that ReSharper suggests a quick-fix that removes all redundant code in the desired scope, including redundant qualifiers, arguments, casts, condition checks, unused assignments, unreachable code, and more.

C#: Introduce optional parameters Example
If the single purpose of an overloaded function is to call the 'implementation' function with default values for some arguments, ReSharper will help you remove this overload and use optional parameters in the 'implementation' function.

C#: Use explicit cast inside foreach loop Example
By design, foreach
allows a hidden cast to a derived type.
On the one hand, this makes it easy to use but on the other hand, this can lead to a
System.InvalidCastException
at runtime.
A quick-fix that ReSharper suggests here helps you make the cast explicit. It is still not safe, but at least it is not hidden anymore.

VB.NET: Specify string comparison Example
A lot of quick-fixes work for multiple languages.
Here is an example of a quick-fix that is available in both C# and VB.NET:
to make clear how exactly two strings are compared, ReSharper suggests replacing equality operator comparison with
String.Equals()
, which will handle casing and make sure that the comparison is culture-aware.

TypeScript: Add type guard Example
TypeScript union types can sometimes be tricky. For example, using a member that is not common to all types in a union type value leads to a compiler error. In this case, ReSharper suggests a number of quick-fixes that add different type guards to disambiguate such a member.

JSON: Add missing required property Example
In JSON files, ReSharper uses its knowledge about all required properties from linked JSON schemas to suggest quick-fixes for missing required properties.

CSS: Remove alpha component Example
CSS Level 3 and below doesn't allow alpha channel in the hexadecimal color notation. Therefore, ReSharper suggests you to either replace hexadecimal color with an RGBA or HSLA color, or just remove the alpha channel value.