Code inspection: The parameter expects a constant for optimal performance
This inspection reports arguments passed to parameters annotated with ConstantExpected when the argument is not a compile-time constant. Such APIs are designed to receive values known at compile time. Passing a variable or another non-constant expression usually defeats the purpose of the annotation and can lead to incorrect API usage.
It is the same as the CA1857 code quality rule.
Example
using System.Diagnostics.CodeAnalysis;
class C
{
static void Log([ConstantExpected] int eventId) { }
void M()
{
int id = GetEventId();
Log(id);
}
}
using System.Diagnostics.CodeAnalysis;
class C
{
static void Log([ConstantExpected] int eventId) { }
void M()
{
Log(42);
}
}
01 April 2026