ReSharper comes with 1200+ quick-fixes that help you instantly repair most of detected code issues in C#, VB.NET, XAML, ASP.NET, and other supported languages. To apply a quick-fix, simply press Alt+Enter over a highlighted code issue, and choose the best way to fix a problem or improve code.
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.
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.
For errors caused by unresolved symbols, ReSharper provides a number of different quick-fixes, for example:
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.
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.
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.
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.
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.
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.
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.
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.
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.
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 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.
In JSON files, ReSharper uses its knowledge about all required properties from linked JSON schemas to suggest quick-fixes for missing required properties.
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.