System.ArgumentNullException Class
Namespace: System
Assembly: System.Runtime.dll
Represents an error that occurs when a null
reference is passed to a method that does not accept it as a valid argument.
Syntax
public class ArgumentNullException : ArgumentException
Constructors
Signature | Description |
---|---|
ArgumentNullException() |
Initializes a new instance with a default error message. |
ArgumentNullException(string paramName) |
Initializes with the name of the parameter that caused the exception. |
ArgumentNullException(string paramName, string message) |
Initializes with a custom message and parameter name. |
ArgumentNullException(string paramName, Exception innerException) |
Initializes with a parameter name and a reference to the inner exception that is the cause of this exception. |
ArgumentNullException(string message, Exception innerException) |
Initializes with a custom message and inner exception. |
Properties
Property | Type | Description |
---|---|---|
ParamName |
string | Gets the name of the parameter that caused the exception. |
Message |
string | Gets a message that describes the current exception. |
InnerException |
Exception | The underlying exception, if any. |
Remarks
Use ArgumentNullException
to indicate that a method argument was null
when it shouldn't be. Providing the parameter name through the constructor helps consumers of the API quickly identify the problematic argument.
Example
public void SetName(string name)
{
if (name == null)
throw new ArgumentNullException(nameof(name));
// Continue processing...
}
This example checks that name
is not null
and throws ArgumentNullException
with the appropriate parameter name.