ReSharper 2024.2 Help

Generate null-checking routines

ReSharper provides plenty of different ways to generate code that checks value-type parameters, expressions, and variables for null. Depending on their purpose, null-checking routines can be divided into two categories:

Null checks for exceptions and assertions

There are situations when encountering an object that is a null reference is critical in your program and should be either logged or signalled by throwing an exception. A typical example here is throwing an ArgumentNullException in a function that is not designed to accept null objects.

Generate null checks for exceptions and assertions

You can generate these kinds of null checks in the following ways:

  • Press Alt+Enter on a parameter or expression and choose the corresponding context action:

    ReSharper: Checking parameter for null
  • If a parameter is marked with the [NotNull] attribute, you can place the caret directly after the parameter name or parameter type and press !:

    private void Foo([NotNull] object/*!*/ arg/*!*/)
  • When generating a constructor (Alt+Insert | Constructor), select Check parameters for null in the dialog.

  • To generate an assertion for null for any nullable expression, ReSharper provides the Assert expression for null action on Alt+Enter. Depending on the nullability analysis settings, it appears as a quick-fix or a context action.

    ReSharper: Asserting expression for null

    This action becomes unavailable if ReSharper infers that the expression can never be null.

If you use code annotations in your project, ReSharper will decorate parameters, which you check for null, with the [NotNull] attribute. This will let ReSharper notify you when null objects are passed to the decorated parameters.

You can disable adding [NotNull] by clearing the Automatically propagate annotations checkbox on the Code Inspection | Code Annotations page of ReSharper options Alt+R, O.

Configuring null checks for exceptions and assertions

This kind of null checks can be written in different ways, so they are configurable on the Code Editing | C# | Null checking page of ReSharper options Alt+R, O, which is also accessible from the Alt+Enter menu on the corresponding actions:

ReSharper: a shortcut for configuring null-check pattern

This options page lists all predefined null-checking patterns in the priority order with higher priority patterns shown on the top. When ReSharper generates a null check, it will take the highest-priority pattern which semantically suits the context, taking into account the current C# version.

By default, ReSharper automatically detects C# version based on the associated compiler. However, you can specify the target C# version explicitly for a project — right-click the project in the Solution Explorer, choose Edit project item properties from the context menu and use the C# Language Level selector .

To set the C# version for all projects in your solution, specify it in a Directory.Build.props file in your solution directory as described here.

For example, with the default configuration the 'throw expression' pattern $EXPR$ ?? new System.ArgumentNullException($NAME$); has higher priority than the 'classic' throw statement if ($EXPR$ == null) throw new System.ArgumentNullException($NAME$);. But if expressions are not allowed in the current context, ReSharper will skip the first and use the second pattern:

// Press Alt+Enter on 'arg' and choose 'Check parameter for null' private void Foo(object arg) { // as expression is not allowed here, the statement is generated: if (arg == null) throw new ArgumentNullException(nameof(arg)); } // However, in the following case the expression is already there private object myField; private void Foo(object arg) { myField = arg; } // so ReSharper will use the first pattern: private object myField; private void Foo(object arg) { myField = arg ?? throw new ArgumentNullException(nameof(arg)); }

The same applies to generating assertions: ReSharper will use the first pattern that is marked with Can use for assertion.

If you have any preferences for generating null checks, use the Move up Alt+U/Move down Alt+D buttons on the options page to raise the priority of patterns that you prefer.

Creating custom null checks for exceptions and assertions

If your codebase provides dedicated helper methods for handling null checks, you may want to create your own null-checking patterns by editing the two custom patterns that are highlighted in bold on the options page — Custom (statement) and Custom (expression):

ReSharper: custom patterns for null checking

By default, these two patterns have the lowest priority, meaning that they will never be used for generation. So if you are going to use them, move them up to raise their priority.

When a custom pattern is selected in the list, you can edit it in the text field at the bottom of the page using the $EXPR$, $NAME$, $MESSAGE$ placeholders. As long as the pattern is valid, ReSharper will display the corresponding icon below the text field.

You can also tick the Can use for assertion checkbox to make the pattern work with the Assert expression for null action.

Null checks to skip objects with null values

Another kind of null checking is used to avoid member access on objects that are null references in cases where skipping such objects is acceptable for your program:

if (expr != null) { DoSomething(); } // or, starting from C#6.0, with the conditional access operator: (expr)?.DoSomething();

To generate such checks, ReSharper provides Check expression for null and Use conditional access actions on Alt+Enter. Depending on the nullability analysis settings, these actions appear as quick-fixes or context actions.

ReSharper: Check expression for null quick-fix

However, these actions are unavailable if ReSharper infers that the expression can never return null, for example, if the corresponding item is marked with the [NotNull] attribute or if the expression is already checked for null.

This feature is supported in the following languages and technologies:

Language: C#

Language: VB.NET

Language: C++

Language: HTML

Language: ASP.NET

Language: Razor

Language: JavaScript

Language: TypeScript

Language: CSS

Language: XML

Language: XAML

Language: Resx

Language: Build Scripts

Language: Protobuf

Language: JSON

Feature is available in C#

Feature is not available in Visual Basic

Feature is not available in C++

Feature is not available in HTML

Feature is not available in ASP.NET

Feature is not available in Razor

Feature is not available in JavaScript

Feature is not available in TypeScript

Feature is not available in CSS

Feature is not available in XML

Feature is not available in XAML

Feature is not available in Resource files

Feature is not available in build script files

Feature is not available in Protobuf

Feature is not available in JSON

Last modified: 25 September 2024