다음을 통해 공유


public 메서드의 인수의 유효성을 검사하십시오.

업데이트: 2007년 11월

TypeName

ValidateArgumentsOfPublicMethods

CheckId

CA1062

범주

Microsoft.Design

변경 수준

주요 변경 아님

원인

외부에서 볼 수 있는 메서드에서 해당 인수가 null(Visual Basic의 경우 Nothing)인지 여부를 확인하지 않고 참조 인수 중 하나를 역참조합니다.

규칙 설명

외부에서 볼 수 있는 메서드에 전달되는 모든 참조 인수는 null인지 여부를 검사해야 합니다. 적절한 경우 인수가 null이면 System.ArgumentNullException을 throw합니다.

위반 문제를 해결하는 방법

이 규칙 위반 문제를 해결하려면 각 참조 인수가 null인지 확인합니다.

경고를 표시하지 않는 경우

이 규칙에서는 경고를 표시해야 합니다.

예제

다음 예제에서는 이 규칙을 위반하는 메서드와 규칙을 충족하는 메서드를 보여 줍니다.

Imports System

Namespace DesignLibrary

    Public Class Test

        ' This method violates the rule.
        Sub DoNotValidate(ByVal input As String)

            If input.Length <> 0 Then
                Console.WriteLine(input)
            End If

        End Sub

        ' This method satisfies the rule.
        Sub Validate(ByVal input As String)

            If input Is Nothing Then
                Throw New ArgumentNullException("input")
            End If

            If input.Length <> 0 Then
                Console.WriteLine(input)
            End If

        End Sub

    End Class

End Namespace
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);
            }
        }
    }
}

Visual Studio 2005에서 이 규칙에는 몇 가지 제한 사항이 있습니다. 그 중 하나는 다음과 같습니다.

유효성을 검사하는 다른 메서드로 매개 변수가 전달되는 경우를 감지할 수 없습니다.

Public Function Method(ByVal value As String) As String
    EnsureNotNull(value)

    ' Fires incorrectly    
    Return value.ToString()
End Function

Private Sub EnsureNotNull(ByVal value As String)
    If value Is Nothing Then
        Throw (New ArgumentNullException("value"))
    End If
End Sub
public string Method(string value)
{
    EnsureNotNull(value);

    // Fires incorrectly    
    return value.ToString();
}

private void EnsureNotNull(string value)
{
    if (value == null)
        throw new ArgumentNullException("value");
}

또한 단락(short-circuit) 연산자가 인식되지 않는다는 제한 사항이 있습니다.

Public Function Method(ByVal value1 As String, ByVal value2 As String) As String
    If value1 Is Nothing OrElse value2 Is Nothing Then
        Throw New ArgumentNullException()
    End If

    ' Fires incorrectly    
    Return value1.ToString() + value2.ToString()

End Function
public string Method(string value1, string value2)
{
    if (value1 == null || value2 == null)
        throw new ArgumentNullException(value1 == null ? "value1" : "value2");

    // Fires incorrectly    
    return value1.ToString() + value2.ToString();
}