ReSharper
 
Get ReSharper
Get your hands on the new features ahead of the release by joining the Early Access Program for ReSharper 2025.1! Learn more

Code inspection: Join null check with assignment

Last modified: 08 April 2024

This inspection supports throw expressions, a new syntax introduced in C# 7.0. A throw expression allows throwing an exception in the middle of another expression, so throwing can now be combined with other tasks such as null-checking. This means that a common operation of checking an argument for null before assigning its value to a variable can now have a more compact look.

In the example below, ReSharper uses a null-coalescing operator to join assignment, checking for null, and throwing an exception into a single statement.