This documentation is archived and is not being maintained.

Validate arguments of public methods







Breaking Change


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

All reference arguments passed to externally visible methods should be checked against null. If appropriate, throw a System.ArgumentNullException when the argument is null.

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

Do not exclude a warning from this rule.

The following example shows a method that violates the rule and a method that satisfies the rule.

Imports System
Namespace DesignLibrary

   Public Class Test
      ' This method violates the rule.
      Sub DoNotValidate(input As String)
         If input.Length <> 0
         End If

      End Sub

      ' This method satisfies the rule.
      Sub Validate(input As String)
         If input Is Nothing
            Throw New ArgumentNullException("input")
         End If

         If input.Length <> 0
         End If

      End Sub

   End Class

End Namespace

In Visual Studio 2005, this rule has a number of limitations. One limitations is that

it does not detect that parameters are being passed to another method that does the validation.

Another limitation is that it does not understand short circuit operators.