Validate arguments of public methods
Null-check reference parameters of externally visible methods.
Microsoft docsDescription
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
public void Print(Order order) => Console.WriteLine(order.Id);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.