
Inline adoc files when they are included exactly once. Also fix language tags because this inlining gives us better information on what language the code is written in.
76 lines
1.4 KiB
Plaintext
76 lines
1.4 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)
|
|
|
|
=== Message
|
|
|
|
Remove unassigned [auto-property|field] "xxx", or set its value.
|
|
|
|
|
|
=== Highlighting
|
|
|
|
property/field name
|
|
|
|
|
|
'''
|
|
== Comments And Links
|
|
(visible only on this page)
|
|
|
|
=== supercedes: S2332
|
|
|
|
=== on 10 Dec 2015, 15:51:43 Ann Campbell wrote:
|
|
Please supply code samples [~tamas.vajk]
|
|
|
|
=== on 11 Dec 2015, 08:45:55 Tamas Vajk wrote:
|
|
\[~ann.campbell.2] WDYT?
|
|
|
|
=== on 11 Dec 2015, 13:57:33 Ann Campbell wrote:
|
|
Looks good
|
|
|
|
endif::env-github,rspecator-view[]
|