System.ArgumentNullException

Class

public sealed class ArgumentNullException : ArgumentException

The exception that is thrown when an attempt to dereference a pointer or reference to a null reference (Nothing in Visual Basic) has occurred.

Commonly thrown when a method parameter is null but is required to be a non-null reference.

Syntax

public sealed class ArgumentNullException : ArgumentException

Constructors

ArgumentNullException()

public ArgumentNullException()

Initializes a new instance of the ArgumentNullException class.

ArgumentNullException(string paramName)

public ArgumentNullException(string paramName)

Initializes a new instance of the ArgumentNullException class with the name of the parameter that causes this exception.

Parameters

Name Type Description
paramName string The name of the parameter that caused the exception.

ArgumentNullException(string message, Exception innerException)

public ArgumentNullException(string message, Exception innerException)

Initializes a new instance of the ArgumentNullException class with a specified error message and a reference to the inner exception that is the cause of this exception.

Parameters

Name Type Description
message string The error message that explains the reason for this exception.
innerException Exception The exception that is the cause of the current exception. If innerException is not null, the last exception that was thrown in a catch block that caught the exception of the current exception is referenced.

ArgumentNullException(string paramName, string message)

public ArgumentNullException(string paramName, string message)

Initializes a new instance of the ArgumentNullException class with a specified error message and the name of the parameter that causes this exception.

Parameters

Name Type Description
paramName string The name of the parameter that caused the exception.
message string A message that describes the error.

Remarks

An ArgumentNullException exception is thrown when a method is invoked and one of the arguments passed to the method is null, but the method expects a non-null value.

For example, if a method requires a string argument and is passed null, an ArgumentNullException is thrown. The ParamName property of the exception indicates the name of the argument that caused the exception.

You can also throw ArgumentNullException when you need to indicate that a value that cannot be null has been passed as an argument.

In Visual Basic, use Throw New ArgumentNullException("parameterName").

In C#, use throw new ArgumentNullException(nameof(parameterName));

Examples

// C# Example
public void ProcessData(string data)
{
    if (data == null)
    {
        throw new ArgumentNullException(nameof(data));
    }
    // ... process data
}

Inheritance Hierarchy

  • object
  • Exception
  • ArgumentException
  • ArgumentNullException

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread-safe. Any instance members are not guaranteed to be thread-safe.