This rule raises an issue each time a static field is updated from a non-static method or property.
Updating a static field from a non-static method introduces ignificant challenges and potential bugs. Multiple class
instances and threads can access and modify the static field concurrently, leading to unintended consequences for other instances or
threads (unexpected behavior, race conditions and
synchronization problems).
class MyClass
{
private static int count = 0;
public void DoSomething()
{
//...
count++; // Noncompliant: make the enclosing instance property 'static' or remove this set on the 'static' field.
}
}
interface MyInterface
{
private static int count = 0;
public void DoSomething()
{
//...
count++; // Noncompliant: remove this set, which updates a 'static' field from an instance method.
}
}