All rules
CA1857Performance Enabled by default: As warning

The parameter expects a constant for optimal performance

The parameter expects a constant for optimal performance

Microsoft docs

Description

This rule flags places in your code where you:

  • Implement an inherited method that uses the System.Diagnostics.CodeAnalysis.ConstantExpectedAttribute attribute but don't mark your parameter with System.Diagnostics.CodeAnalysis.ConstantExpectedAttribute.
  • Pass a non-constant argument to a parameter that has the System.Diagnostics.CodeAnalysis.ConstantExpectedAttribute attribute.
  • Pass an invalid constant argument to a parameter that has the System.Diagnostics.CodeAnalysis.ConstantExpectedAttribute attribute.
  • Pass a constant argument to a parameter that has the System.Diagnostics.CodeAnalysis.ConstantExpectedAttribute attribute, and the argument is out of range of the System.Diagnostics.CodeAnalysis.ConstantExpectedAttribute.Min or System.Diagnostics.CodeAnalysis.ConstantExpectedAttribute.Max values.

Cause

An invalid argument is passed to a parameter that's annotated with System.Diagnostics.CodeAnalysis.ConstantExpectedAttribute.

How to fix violations

Correct your code as indicated by the specific error message you receive.

Example

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

When to suppress

It's safe to suppress a warning from this rule if performance isn't a concern.

Group results
0 yes 0 no
ConsensusNone (disabled)
Severity preference (yes voters)
Suggestion0
Warning0
Error0