Movatterモバイル変換


[0]ホーム

URL:


Skip to main content

This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Download Microsoft EdgeMore info about Internet Explorer and Microsoft Edge
Table of contentsExit editor mode

CA2264: Do not pass a non-nullable value to 'ArgumentNullException.ThrowIfNull'

Feedback

In this article

PropertyValue
Rule IDCA2264
TitleDo not pass a non-nullable value toArgumentNullException.ThrowIfNull
CategoryUsage
Fix is breaking or non-breakingNon-breaking
Enabled by default in .NET 10As warning

Cause

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.

Rule description

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.

How to fix violations

Remove theArgumentNullException.ThrowIfNull call.

Example

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

When to suppress warnings

It's always safe to suppress this warning.

Suppress a 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 tonone in theconfiguration file.

[*.{cs,vb}]dotnet_diagnostic.CA2264.severity = none

For more information, seeHow to suppress code analysis warnings.

Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, seeour contributor guide.

Feedback

Was this page helpful?

YesNoNo

Need help with this topic?

Want to try using Ask Learn to clarify or guide you through this topic?

Suggest a fix?

  • Last updated on

In this article

Was this page helpful?

YesNo
NoNeed help with this topic?

Want to try using Ask Learn to clarify or guide you through this topic?

Suggest a fix?