rspec/rules/S2328/csharp/rule.adoc
Fred Tingaud 16f6c0aecf
Inline adoc when include has no additional value (#1940)
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.
2023-05-25 14:18:12 +02:00

62 lines
1.3 KiB
Plaintext

== Why is this an issue?
``++GetHashCode++`` is used to file an object in a ``++Dictionary++`` or ``++Hashtable++``. If ``++GetHashCode++`` uses non-``++readonly++`` fields and those fields change after the object is stored, the object immediately becomes mis-filed in the ``++Hashtable++``. Any subsequent test to see if the object is in the ``++Hashtable++`` will return a false negative.
=== Noncompliant code example
[source,csharp]
----
public class Person
{
public int age;
public string name;
public override int GetHashCode()
{
int hash = 12;
hash += this.age.GetHashCode(); // Noncompliant
hash += this.name.GetHashCode(); // Noncompliant
return hash;
}
----
=== Compliant solution
[source,csharp]
----
public class Person
{
public readonly DateTime birthday;
public string name;
public override int GetHashCode()
{
int hash = 12;
hash += this.birthday.GetHashCode();
return hash;
}
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Refactor 'GetHashCode' to not reference mutable fields.
=== Highlighting
* primary: method name
* secondary: uses of mutable fields
** message: Remove this use of "xxx" or make it "readonly".
endif::env-github,rspecator-view[]