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 | CA1802 |
Title | Use Literals Where Appropriate |
Category | Performance |
Fix is breaking or non-breaking | Non-breaking |
Enabled by default in .NET 9 | No |
A field is declared static
and readonly
(Shared
and ReadOnly
in Visual Basic), and is initialized with a value that is computable at compile time.
By default, this rule only looks at externally visible, static, readonly fields, but this is configurable.
The value of a static readonly
field is computed at run time when the static constructor for the declaring type is called. If the static readonly
field is initialized when it is declared and a static constructor is not declared explicitly, the compiler emits a static constructor to initialize the field.
The value of a const
field is computed at compile time and stored in the metadata, which improves run-time performance when it is compared to a static readonly
field.
Because the value assigned to the targeted field is computable at compile time, change the declaration to a const
field so that the value is computed at compile time instead of at run time.
To fix a violation of this rule, replace the static
and readonly
modifiers with the const
modifier.
Notă
The use of the const modifier is not recommended for all scenarios.
It is safe to suppress a warning from this rule, or disable the rule, if performance is not of concern.
Avertisment
For public or externally visible members, changing static readonly
to const
can lead to issues. const
values are embedded in dependent assemblies at compile time, so changes in the library's value might not propagate, potentially causing errors. If the value of your member might change in the future, suppress this rule.
Notă
Using const
is safe for private
members and generally safe for internal
members unless exposed via InternalsVisibleTo
or deployed separately.
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 CA1802
// The code that's violating the rule is on this line.
#pragma warning restore CA1802
To disable the rule for a file, folder, or project, set its severity to none
in the configuration file.
[*.{cs,vb}]
dotnet_diagnostic.CA1802.severity = none
For more information, see How to suppress code analysis warnings.
Use the following options to configure which parts of your codebase to run this rule on.
You can configure these options for just this rule, for all rules they apply to, or for all rules in this category (Performance) that they apply to. For more information, see Code quality rule configuration options.
You can configure which parts of your codebase to run this rule on, based on their accessibility, by setting the api_surface option. For example, to specify that the rule should run only against the non-public API surface, add the following key-value pair to an .editorconfig file in your project:
dotnet_code_quality.CAXXXX.api_surface = private, internal
Notă
Replace the XXXX
part of CAXXXX
with the ID of the applicable rule.
You can configure this rule to override the required field modifiers. By default, static
and readonly
are both required modifiers for fields that are analyzed. You can override this to a comma separated listed of one or more modifier values from the below table:
Option Value | Summary |
---|---|
none |
No modifier requirement. |
static or Shared |
Must be declared as 'static' ('Shared' in Visual Basic). |
const |
Must be declared as 'const'. |
readonly |
Must be declared as 'readonly'. |
For example, to specify that the rule should run against both static and instance fields, add the following key-value pair to an .editorconfig file in your project:
dotnet_code_quality.CA1802.required_modifiers = none
The following example shows a type, UseReadOnly
, that violates the rule and a type, UseConstant
, that satisfies the rule.
Imports System
Namespace ca1802
' This class violates the rule.
Public Class UseReadOnly
Shared ReadOnly x As Integer = 3
Shared ReadOnly y As Double = x + 2.1
Shared ReadOnly s As String = "readonly"
End Class
' This class satisfies the rule.
Public Class UseConstant
Const x As Integer = 3
Const y As Double = x + 2.1
Const s As String = "const"
End Class
End Namespace
// This class violates the rule.
public class UseReadOnly
{
static readonly int x = 3;
static readonly double y = x + 2.1;
static readonly string s = "readonly";
public void Print()
{
Console.WriteLine(s);
}
}
// This class satisfies the rule.
public class UseConstant
{
const int x = 3;
const double y = x + 2.1;
const string s = "const";
}
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