Do not pass a non-nullable value to ArgumentNullException.ThrowIfNull
Do not pass a non-nullable value to ArgumentNullException.ThrowIfNull
Microsoft docsDescription
ArgumentNullException.ThrowIfNull throws when the passed argument is null. Certain constructs like non-nullable structs (except for System.Nullable1), 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. Thus, calling ArgumentNullException.ThrowIfNull` is unnecessary.
In the case of a struct, since ArgumentNullException.ThrowIfNull accepts an object?, the struct is boxed, which causes an additional performance penalty.
Cause
A value that's known to never be null is passed to ArgumentNullException.ThrowIfNull().
How to fix violations
Remove the ArgumentNullException.ThrowIfNull call.
Example
static void Print(int value)
{
ArgumentNullException.ThrowIfNull(value);
Console.WriteLine(value);
}
static void Print(int value)
{
Console.WriteLine(value.Value);
}When to suppress
It's always safe to suppress this warning.