Ask Learn
Preview
Ask Learn is an AI assistant that can answer questions, clarify concepts, and define terms using trusted Microsoft documentation.
Please sign in to use Ask Learn.
Sign inThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Property | Value |
---|---|
Rule ID | CA2264 |
Title | Do not pass a non-nullable value to ArgumentNullException.ThrowIfNull |
Category | Usage |
Fix is breaking or non-breaking | Non-breaking |
Enabled by default in .NET 9 | As warning |
When a value that's known to never be null is passed to ArgumentNullException.ThrowIfNull()
, an exception is never thrown, making the statement a no-op.
ArgumentNullException.ThrowIfNull
throws when the passed argument is null
. Certain constructs like non-nullable structs (except for Nullable<T>), type parameters known to be non-nullable structs, 'nameof()' expressions, and 'new' expressions are known to never be null, so ArgumentNullException.ThrowIfNull
will never throw.
In the case of a struct, since ArgumentNullException.ThrowIfNull
accepts an object?
, the struct is boxed, which causes an additional performance penalty.
Remove the ArgumentNullException.ThrowIfNull
call.
The following code snippet shows a violation of CA2264:
static void Print(int value)
{
ArgumentNullException.ThrowIfNull(value);
Console.WriteLine(value);
}
The following code snippet fixes the violation:
static void Print(int value)
{
Console.WriteLine(value.Value);
}
It's always safe to suppress this warning.
If you just want to suppress a single violation, add preprocessor directives to your source file to disable and then re-enable the rule.
#pragma warning disable CA2264
// The code that's violating the rule is on this line.
#pragma warning restore CA2264
To disable the rule for a file, folder, or project, set its severity to none
in the configuration file.
[*.{cs,vb}]
dotnet_diagnostic.CA2264.severity = none
For more information, see How to suppress code analysis warnings.
.NET feedback
.NET is an open source project. Select a link to provide feedback:
Ask Learn is an AI assistant that can answer questions, clarify concepts, and define terms using trusted Microsoft documentation.
Please sign in to use Ask Learn.
Sign in