2023-05-03 11:06:20 +02:00
== Why is this an issue?
2023-06-07 15:41:34 +02:00
Unlike instance fields, which can only be accessed by code having a hold on the instance, `static` fields can be accessed by any code having visibility of the field and its type.
2023-02-22 15:19:19 +01:00
2023-06-05 16:49:02 +02:00
[source,csharp,diff-id=1,diff-type=noncompliant]
----
public class Math
{
public static double Pi = 3.14; // Noncompliant
}
2023-02-22 15:19:19 +01:00
2023-06-05 16:49:02 +02:00
// Somewhere else, where Math and Math.Pi are visible
var pi = Math.Pi; // Reading the value
Math.Pi = 3.1416; // Mutating the value
----
2021-04-28 16:49:39 +02:00
2023-06-07 15:41:34 +02:00
Another typical scenario of the use of a non-private mutable `static` field is the following:
2021-04-28 18:08:03 +02:00
2023-06-05 16:49:02 +02:00
[source,csharp,diff-id=2,diff-type=noncompliant]
----
public class Shape
{
public static Shape Empty = new EmptyShape(); // Noncompliant
2021-04-28 16:49:39 +02:00
2023-06-05 16:49:02 +02:00
private class EmptyShape : Shape
{
}
}
----
Non-private `static` fields that are neither `const` nor `readonly`, like the ones in the examples above, can lead to errors and unpredictable behavior.
This can happen because:
* Any object can modify these fields and alter the global state. This makes the code more difficult to read, debug and test.
+
2022-02-04 17:28:24 +01:00
[source,csharp]
2021-04-28 16:49:39 +02:00
----
2023-06-05 16:49:02 +02:00
class Counters
{
public static int ErrorCounter = 0;
}
class Program
2021-04-28 16:49:39 +02:00
{
2023-06-05 16:49:02 +02:00
public static void Thread1()
{
// ...
Counters.ErrorCounter = 0; // Error counter reset
// ...
}
public static void Thread2()
{
// ...
if (Counters.ErrorCounter > 0)
{
Trace.TraceError($"There are {Counters.ErrorCounter} errors"); // It may print "There are 0 errors"
}
// ...
}
2023-02-22 15:19:19 +01:00
}
2021-04-28 16:49:39 +02:00
----
2023-06-05 16:49:02 +02:00
* Correctly accessing these fields from different threads needs synchronization with `lock` or equivalent mechanisms. Improper synchronization may lead to unexpected results.
+
2022-02-04 17:28:24 +01:00
[source,csharp]
2021-04-28 16:49:39 +02:00
----
2023-06-05 16:49:02 +02:00
class Counters
2021-04-28 16:49:39 +02:00
{
2023-06-05 16:49:02 +02:00
public static volatile int ErrorCounter;
}
2021-04-28 16:49:39 +02:00
2023-06-05 16:49:02 +02:00
class Program
{
public static void ImproperSynchronization()
{
Counters.ErrorCounter = 0;
Parallel.ForEach(Enumerable.Range(0, 1000), _ => Counters.ErrorCounter++); // Volatile is not enough
Console.WriteLine(Counters.ErrorCounter); // May print less than 1000
}
public static void ProperSynchronization()
{
Counters.ErrorCounter = 0;
Parallel.ForEach(Enumerable.Range(0, 1000), _ => Interlocked.Increment(ref Counters.ErrorCounter));
Console.WriteLine(Counters.ErrorCounter); // Always prints 1000
}
2023-02-22 15:19:19 +01:00
}
2021-04-28 16:49:39 +02:00
----
2023-06-05 16:49:02 +02:00
Publicly visible `static` fields should only be used to store shared data that does not change.
To enforce this intent, these fields should be marked `readonly` or converted to `const`.
2021-04-28 18:08:03 +02:00
2023-06-05 16:49:02 +02:00
[source,csharp,diff-id=1,diff-type=compliant]
2021-04-28 16:49:39 +02:00
----
public class Math
{
2023-06-05 16:49:02 +02:00
public const double Pi = 3.14;
2023-02-22 15:19:19 +01:00
}
2021-04-28 16:49:39 +02:00
----
2023-06-05 16:49:02 +02:00
[source,csharp,diff-id=2,diff-type=compliant]
2021-04-28 16:49:39 +02:00
----
public class Shape
{
2023-06-05 16:49:02 +02:00
public static readonly Shape Empty = new EmptyShape();
private class EmptyShape : Shape
{
}
2023-02-22 15:19:19 +01:00
}
2021-04-28 16:49:39 +02:00
----
2021-04-28 18:08:03 +02:00
2023-06-05 16:49:02 +02:00
include::../resources-dotnet.adoc[]
2021-06-02 20:44:38 +02:00
2021-06-03 09:05:38 +02:00
ifdef::env-github,rspecator-view[]
2021-09-20 15:38:42 +02:00
'''
== Implementation Specification
(visible only on this page)
2023-05-25 14:18:12 +02:00
=== Message
Change the visibility of "xxx" or make it "const" or "readonly".
2021-06-08 15:52:13 +02:00
'''
2021-06-02 20:44:38 +02:00
== Comments And Links
(visible only on this page)
2023-06-05 16:49:02 +02:00
include::../comments-and-links.adoc[]
2023-05-25 14:18:12 +02:00
2021-06-03 09:05:38 +02:00
endif::env-github,rspecator-view[]