
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.
63 lines
1.3 KiB
Plaintext
63 lines
1.3 KiB
Plaintext
== Why is this an issue?
|
|
|
|
There is a contract between ``++Equals(object)++`` and ``++GetHashCode()++``: If two objects are equal according to the ``++Equals(object)++`` method, then calling ``++GetHashCode()++`` on each of them must yield the same result. If this is not the case, many collections won't handle class instances correctly.
|
|
|
|
|
|
In order to comply with the contract, ``++Equals(object)++`` and ``++GetHashCode()++`` should be either both inherited, or both overridden.
|
|
|
|
=== Noncompliant code example
|
|
|
|
[source,csharp]
|
|
----
|
|
class MyClass // Noncompliant - should also override "GetHashCode()"
|
|
{
|
|
public override bool Equals(object obj)
|
|
{
|
|
// ...
|
|
}
|
|
}
|
|
----
|
|
|
|
=== Compliant solution
|
|
|
|
[source,csharp]
|
|
----
|
|
class MyClass
|
|
{
|
|
public override bool Equals(object obj)
|
|
{
|
|
// ...
|
|
}
|
|
|
|
public override int GetHashCode()
|
|
{
|
|
// ...
|
|
}
|
|
}
|
|
----
|
|
|
|
include::../see.adoc[]
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
=== Message
|
|
|
|
This [class|struct] overrides "GetHashCode()|Equals()" and should therefore also override "GetHashCode()|Equals()".
|
|
|
|
|
|
=== Highlighting
|
|
|
|
The [class|struct] name.
|
|
|
|
|
|
'''
|
|
== Comments And Links
|
|
(visible only on this page)
|
|
|
|
include::../comments-and-links.adoc[]
|
|
endif::env-github,rspecator-view[]
|