Hendelser
17. mars, 21 - 21. mars, 10
Bli med i meetup-serien for å bygge skalerbare AI-løsninger basert på virkelige brukstilfeller med andre utviklere og eksperter.
Registrer deg nåDenne nettleseren støttes ikke lenger.
Oppgrader til Microsoft Edge for å dra nytte av de nyeste funksjonene, sikkerhetsoppdateringene og den nyeste tekniske støtten.
Property | Value |
---|---|
Rule ID | CA1512 |
Title | Use ArgumentOutOfRangeException throw helper |
Category | Maintainability |
Fix is breaking or non-breaking | Non-Breaking |
Enabled by default in .NET 9 | As suggestion |
Code checks whether an argument is less than or greater than a given value and then conditionally throws an ArgumentOutOfRangeException.
Argument checks have a substantial impact on code size and often dominate the code for small functions and property setters. These checks prevent inlining and cause substantial instruction-cache pollution. Throw-helper methods such as ArgumentOutOfRangeException.ThrowIfGreaterThan are simpler and more efficient than if
blocks that construct a new exception instance.
The following code snippet shows violations of CA1512:
void M(int arg)
{
if (arg is 0)
throw new ArgumentOutOfRangeException(nameof(arg));
if (arg < 0)
throw new ArgumentOutOfRangeException(nameof(arg));
if (arg <= 0)
throw new ArgumentOutOfRangeException(nameof(arg));
if (arg <= 42)
throw new ArgumentOutOfRangeException(nameof(arg));
if (arg < 42)
throw new ArgumentOutOfRangeException(nameof(arg));
if (arg > 42)
throw new ArgumentOutOfRangeException(nameof(arg));
if (arg >= 42)
throw new ArgumentOutOfRangeException(nameof(arg));
if (arg == 42)
throw new ArgumentOutOfRangeException(nameof(arg));
if (arg != 42)
throw new ArgumentOutOfRangeException(nameof(arg));
}
The following code snippet shows the fixes:
void M(int arg)
{
ArgumentOutOfRangeException.ThrowIfZero(arg);
ArgumentOutOfRangeException.ThrowIfNegative(arg);
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(arg);
ArgumentOutOfRangeException.ThrowIfLessThanOrEqual(arg, 42);
ArgumentOutOfRangeException.ThrowIfLessThan(arg, 42);
ArgumentOutOfRangeException.ThrowIfGreaterThan(arg, 42);
ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(arg, 42);
ArgumentOutOfRangeException.ThrowIfEqual(arg, 42);
ArgumentOutOfRangeException.ThrowIfNotEqual(arg, 42);
}
Replace the if
block that throws the exception with a call to one of the following throw-helper methods:
Or, in Visual Studio, use the lightbulb menu to fix your code automatically.
It's safe to suppress a violation of this rule if you're not concerned about the maintainability of your code. It is also fine to suppress violations that are identified to be false positives.
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 CA1512
// The code that's violating the rule is on this line.
#pragma warning restore CA1512
To disable the rule for a file, folder, or project, set its severity to none
in the configuration file.
[*.{cs,vb}]
dotnet_diagnostic.CA1512.severity = none
For more information, see How to suppress code analysis warnings.
.NET-tilbakemelding
.NET er et åpen kilde-prosjekt. Velg en kobling for å gi tilbakemelding:
Hendelser
17. mars, 21 - 21. mars, 10
Bli med i meetup-serien for å bygge skalerbare AI-løsninger basert på virkelige brukstilfeller med andre utviklere og eksperter.
Registrer deg nå