This 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 trysigning in orchanging directories.
Access to this page requires authorization. You can trychanging directories.
| Property | Value |
|---|---|
| Rule ID | CA2264 |
| Title | Do not pass a non-nullable value toArgumentNullException.ThrowIfNull |
| Category | Usage |
| Fix is breaking or non-breaking | Non-breaking |
| Enabled by default in .NET 10 | As warning |
When a value that's known to never be null is passed toArgumentNullException.ThrowIfNull(), an exception is never thrown, making the statement a no-op.
ArgumentNullException.ThrowIfNull throws when the passed argument isnull. Certain constructs like non-nullable structs (except forNullable<T>), type parameters known to be non-nullable structs, 'nameof()' expressions, and 'new' expressions are known to never be null, soArgumentNullException.ThrowIfNull will never throw.
In the case of a struct, sinceArgumentNullException.ThrowIfNull accepts anobject?, the struct is boxed, which causes an additional performance penalty.
Remove theArgumentNullException.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 CA2264To disable the rule for a file, folder, or project, set its severity tonone in theconfiguration file.
[*.{cs,vb}]dotnet_diagnostic.CA2264.severity = noneFor more information, seeHow to suppress code analysis warnings.
Was this page helpful?
Need help with this topic?
Want to try using Ask Learn to clarify or guide you through this topic?
Was this page helpful?
Want to try using Ask Learn to clarify or guide you through this topic?