org.sonar.plugins.csharp.S2357.html Maven / Gradle / Ivy
Why is this an issue?
Fields should not be part of an API, and therefore should always be private. Indeed, they cannot be added to an interface for instance, and
validation cannot be added later on without breaking backward compatibility. Instead, developers should encapsulate their fields into properties.
Explicit property getters and setters can be introduced for validation purposes or to smooth the transition to a newer system.
Noncompliant code example
public class Foo
{
public int MagicNumber = 42;
}
Compliant solution
public class Foo
{
public int MagicNumber
{
get { return 42; }
}
}
or
public class Foo
{
private int MagicNumber = 42;
}
Exceptions
struct
s are ignored, as are static
and const
fields in classes.
Further, an issue is only raised when the real accessibility is public
, taking into account the class accessibility.
© 2015 - 2024 Weber Informatics LLC | Privacy Policy