CA1062: Validate arguments of public methods

 

For the latest documentation on Visual Studio 2017, see Visual Studio 2017 Documentation.

For the latest documentation on Visual Studio 2017, see CA1062: Validate arguments of public methods on docs.microsoft.com.

TypeNameValidateArgumentsOfPublicMethods
CheckIdCA1062
CategoryMicrosoft.Design
Breaking ChangeNon Breaking

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

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

If a method can be called from an unknown assembly because it is declared public or protected, you should validate all parameters of the method. If the method is designed to be called only by known assemblies, you should make the method internal and apply the InternalsVisibleToAttribute attribute to the assembly that contains the method.

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

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.

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

In Visual Studio 2005, this rule does not detect that parameters are being passed to another method that does the validation.

Copy constructors that populate field or properties that are reference objects can also violate the CA1062 rule. The violation occurs because the copied object that is passed to the copy constructor might be null (Nothing in Visual Basic). To resolve the violation, use a static (Shared in Visual Basic) method to check that the copied object is not null.

In the following Person class example, the other object that is passed to the Person copy constructor might be null.

  
public class Person  
{  
    public string Name { get; private set; }  
    public int Age { get; private set; }  
  
    public Person(string name, int age)  
    {  
        Name = name;  
        Age = age;  
    }  
  
    // Copy constructor CA1062 fires because other is dereferenced  
    // without being checked for null  
    public Person(Person other)  
        : this(other.Name, other.Age)  
    {  
    }  
}  
  

In the following revised Person example, the other object that is passed to the copy constructor is first checked for null in the PassThroughNonNull method.

public class Person  
{  
    public string Name { get; private set; }  
    public int Age { get; private set; }  
  
    public Person(string name, int age)  
    {  
        Name = name;  
        Age = age;  
    }  
  
    // Copy constructor  
    public Person(Person other)  
        : this(PassThroughNonNull(other).Name,   
          PassThroughNonNull(other).Age)  
    {   
    }  
  
    // Null check method  
    private static Person PassThroughNonNull(Person person)  
    {  
        if (person == null)  
            throw new ArgumentNullException("person");  
        return person;  
    }  
}  
  

Show: