60 lines
1.2 KiB
Plaintext
60 lines
1.2 KiB
Plaintext
== 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
|
|
|
|
[source,csharp]
|
|
----
|
|
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
|
|
|
|
[source,csharp]
|
|
----
|
|
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.
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
include::message.adoc[]
|
|
|
|
include::highlighting.adoc[]
|
|
|
|
'''
|
|
== Comments And Links
|
|
(visible only on this page)
|
|
|
|
include::comments-and-links.adoc[]
|
|
endif::env-github,rspecator-view[]
|