CA1857: The parameter expects a constant for optimal performance
Property | Value |
---|---|
Rule ID | CA1857 |
Title | The parameter expects a constant for optimal performance |
Category | Performance |
Fix is breaking or non-breaking | Non-breaking |
Enabled by default in .NET 9 | As warning |
Cause
An invalid argument is passed to a parameter that's annotated with ConstantExpectedAttribute.
Rule description
This rule flags places in your code where you:
- Implement an inherited method that uses the ConstantExpectedAttribute attribute but don't mark your parameter with ConstantExpectedAttribute.
- Pass a non-constant argument to a parameter that has the ConstantExpectedAttribute attribute.
- Pass an invalid constant argument to a parameter that has the ConstantExpectedAttribute attribute.
- Pass a constant argument to a parameter that has the ConstantExpectedAttribute attribute, and the argument is out of range of the Min or Max values.
How to fix violations
Correct your code as indicated by the specific error message you receive.
Example 1 (attribute expected)
The following code snippet shows a violation of CA1857:
public interface I1<T>
{
T M1(T operand1, [ConstantExpected] T operand2);
}
public class C1 : I1<int>
{
public int M1(int operand1, int operand2) =>
throw new NotImplementedException();
}
The following code snippet fixes the violation:
public interface I1<T>
{
T M1(T operand1, [ConstantExpected] T operand2);
}
public class C1 : I1<int>
{
public int M1(int operand1, [ConstantExpected] int operand2) =>
throw new NotImplementedException();
}
Example 2 (constant not constant)
The following code snippet shows a violation of CA1857:
static void M1(int i) => M2(i);
static void M2([ConstantExpected] int i) { }
The following code snippet fixes the violation:
static void M1([ConstantExpected] int i) => M2(i);
static void M2([ConstantExpected] int i) { }
Example 3 (invalid constant)
The following code snippet shows a violation of CA1857:
static void M1() => M2((string)(object)20);
static void M2([ConstantExpected] string s) { }
The following code snippet fixes the violation:
static void M1() => M2("20");
static void M2([ConstantExpected] string s) { }
Example 4 (constant out of bounds)
The following code snippet shows a violation of CA1857:
static void M1() => M2(5);
static void M2([ConstantExpected(Min = 3, Max = 4)] int i) { }
The following code snippet fixes the violation:
static void M1() => M2(4);
static void M2([ConstantExpected(Min = 3, Max = 4)] int i) { }
When to suppress warnings
It's safe to suppress a warning from this rule if performance isn't a concern.
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 CA1857
// The code that's violating the rule is on this line.
#pragma warning restore CA1857
To disable the rule for a file, folder, or project, set its severity to none
in the configuration file.
[*.{cs,vb}]
dotnet_diagnostic.CA1857.severity = none
For more information, see How to suppress code analysis warnings.