The requested page is not available for the requested platform. You are viewing the content for Default platform.

CRR0017 - Null check follows usage

This analyzer detects logical expressions in which the null-check is performed after the value-check. Since boolean operators in C# and Visual Basic are short-circuit, the order of operands determines their execution order. Refer to the following code snippet.

if(str.Length < 2 || str == null) // CRR0017
    DoSomething();

In this example, an attempt to get the str.Length value will be made before checking that the str object is null (Nothing in VB). This will cause the System.NullReferenceException exception in case the str object is null (Nothing in VB). To avoid this, swap the operands so that the null-check is performed before any other checks. The execution will break as soon as one of the OR operands returns true.

if (str == null || str.Length < 2)
    DoSomething();