All rules
CA1062Design Enabled by default: No MS default: Warning

Validate arguments of public methods

Null-check reference parameters of externally visible methods.

Microsoft docs

Description

Externally visible methods should validate their reference-type arguments against null before dereferencing them.

Cause

An externally visible method dereferences one of its reference arguments without verifying whether that argument is null (Nothing in Visual Basic).

You can configure this rule to exclude certain types and parameters from analysis. You can also indicate null-check validation methods.

Why it matters

Validating arguments produces clear ArgumentNullExceptions instead of confusing NullReferenceExceptions deep in the call stack.

How to fix violations

To fix a violation of this rule, validate each reference argument against null.

Examples

Avoid
public void Print(Order order) => Console.WriteLine(order.Id);
Prefer
public void Print(Order order)
{
    ArgumentNullException.ThrowIfNull(order);
    Console.WriteLine(order.Id);
}

When to suppress

You can suppress a warning from this rule if you are sure that the dereferenced parameter has been validated by another method call in the function.

Group results
0 yes 0 no
ConsensusNone (disabled)
Severity preference (yes voters)
Suggestion0
Warning0
Error0