GU0023
November 24, 2021 ยท View on GitHub
Static members that initialize with other static members depend on document order
| Topic | Value |
|---|---|
| Id | GU0023 |
| Severity | Warning |
| Enabled | True |
| Category | Gu.Analyzers.Correctness |
| Code | GU0023StaticMemberOrderAnalyzer |
Description
Static members that initialize with other static members depend on document order.
Motivation
A static field or property initialized with another must be declared after.
public class Foo
{
public static readonly int Value1 = Value2;
public static readonly int Value2 = 2;
}
In the above example Value1 will be initialized with 0 which is likely not the intention.
How to fix violations
Declare members with dependencies in correct order.
public class Foo
{
public static readonly int Value2 = 2;
public static readonly int Value1 = Value2;
}
Configure severity
Via ruleset file.
Configure the severity per project, for more info see MSDN.
Via #pragma directive.
#pragma warning disable GU0023 // Static members that initialize with other static members depend on document order
Code violating the rule here
#pragma warning restore GU0023 // Static members that initialize with other static members depend on document order
Or put this at the top of the file to disable all instances.
#pragma warning disable GU0023 // Static members that initialize with other static members depend on document order
Via attribute [SuppressMessage].
[System.Diagnostics.CodeAnalysis.SuppressMessage("Gu.Analyzers.Correctness",
"GU0023:Static members that initialize with other static members depend on document order",
Justification = "Reason...")]