Подія
17 бер., 21 - 21 бер., 10
Приєднайтеся до серії нарад, щоб створити масштабовані рішення зі ШІ на основі реальних випадків використання з колегами-розробниками та експертами.
Зареєструватися заразЦей браузер більше не підтримується.
Замініть його на Microsoft Edge, щоб користуватися перевагами найновіших функцій, оновлень безпеки та технічної підтримки.
Property | Value |
---|---|
Rule ID | CA2207 |
Title | Initialize value type static fields inline |
Category | Usage |
Fix is breaking or non-breaking | Non-breaking |
Enabled by default in .NET 9 | No |
A value-type declares an explicit static constructor.
When a value-type is declared, it undergoes a default initialization where all value-type fields are set to zero and all reference-type fields are set to null
(Nothing
in Visual Basic). An explicit static constructor is only guaranteed to run before an instance constructor or static member of the type is called. Therefore, if the type is created without calling an instance constructor, the static constructor is not guaranteed to run.
If all static data is initialized inline and no explicit static constructor is declared, the C# and Visual Basic compilers add the beforefieldinit
flag to the CIL class definition. The compilers also add a private static constructor that contains the static initialization code. This private static constructor is guaranteed to run before any static fields of the type are accessed.
To fix a violation of this rule initialize all static data when it is declared and remove the static constructor.
Do not suppress a warning from this rule.
Відгук про .NET
.NET – це проект із відкритим кодом. Виберіть посилання, щоб надати відгук:
Подія
17 бер., 21 - 21 бер., 10
Приєднайтеся до серії нарад, щоб створити масштабовані рішення зі ШІ на основі реальних випадків використання з колегами-розробниками та експертами.
Зареєструватися зараз