org.sonar.plugins.csharp.S3459.html Maven / Gradle / Ivy
Why is this an issue?
Fields and auto-properties that are never assigned to hold the default values for their types. They are either pointless code or, more likely,
mistakes.
Noncompliant code example
class MyClass
{
private int field; // Noncompliant, shouldn't it be initialized? This way the value is always default(int), 0.
private int Property { get; set; } // Noncompliant
public void Print()
{
Console.WriteLine(field); //Will always print 0
Console.WriteLine(Property); //Will always print 0
}
}
Compliant solution
class MyClass
{
private int field = 1;
private int Property { get; set; } = 42;
public void Print()
{
field++;
Console.WriteLine(field);
Console.WriteLine(Property);
}
}
Exceptions
- Fields on types decorated with
System.SerializableAttribute
attribute.
© 2015 - 2024 Weber Informatics LLC | Privacy Policy