All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonar.plugins.csharp.S2696.html Maven / Gradle / Ivy

There is a newer version: 10.2.0.105762
Show newest version

This rule raises an issue each time a static field is updated from a non-static method or property.

Why is this an issue?

Updating a static field from a non-static method introduces significant 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.
  }
}

Resources

Documentation

Articles & blog posts

Standards





© 2015 - 2024 Weber Informatics LLC | Privacy Policy