Evenimente
17 mar., 21 - 21 mar., 10
Alăturați-vă seriei de întâlniri pentru a construi soluții AI scalabile bazate pe cazuri de utilizare din lumea reală cu colegi dezvoltatori și experți.
Înregistrați-vă acumAcest browser nu mai este acceptat.
Faceți upgrade la Microsoft Edge pentru a profita de cele mai noi funcții, actualizări de securitate și asistență tehnică.
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 |
An invalid argument is passed to a parameter that's annotated with ConstantExpectedAttribute.
This rule flags places in your code where you:
Correct your code as indicated by the specific error message you receive.
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();
}
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) { }
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) { }
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) { }
It's safe to suppress a warning from this rule if performance isn't a concern.
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.
Feedback pentru .NET
.NET este un proiect open source. Selectați un link pentru a oferi feedback:
Evenimente
17 mar., 21 - 21 mar., 10
Alăturați-vă seriei de întâlniri pentru a construi soluții AI scalabile bazate pe cazuri de utilizare din lumea reală cu colegi dezvoltatori și experți.
Înregistrați-vă acum