Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Validate arguments of public methods
Collapse the table of content
Expand the table of content

Validate arguments of public methods

TypeName

ValidateArgumentsOfPublicMethods

CheckId

CA1062

Category

Microsoft.Design

Breaking Change

NonBreaking

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.

using System;
  
namespace DesignLibrary
{
   public class Test
   {
      // This method violates the rule.
      public void DoNotValidate(string input)
      {
         if( input.Length != 0 )
         {
            Console.WriteLine(input);
         }
      }

      // This method satisfies the rule.
      public void Validate(string input)
      {
         if( input == null )
         {
            throw new ArgumentNullException("input");
         }
         if( input.Length != 0 )
         {
            Console.WriteLine(input);
         }
      }
   }
}

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.

Community Additions

ADD
Show:
© 2015 Microsoft