A type implements IEquatable<T>, but does not override Equals method.
Rule descriptionA type implementing IEquatable<T> interface indicates that it supports comparing two instances of the type for equality. You should also override the base class implementations of Equals and GetHashCode() methods so that their behavior is consistent with that of the System.IEquatable<T>.Equals implementation. For more information, see Notes to implementers.
Your Equals implementation should return results that are consistent with System.IEquatable<T>.Equals implementation.
How to fix violationsTo fix a violation, override Equals and implement it by invoking the System.IEquatable<T>.Equals implementation. For example, the following two code snippets show a violation of the rule and how to fix it:
using System;
public struct S : IEquatable<S>
{
private readonly int _value;
public S(int f)
{
_value = f;
}
public bool Equals(S other)
=> _value == other._value;
}
using System;
public struct S : IEquatable<S>
{
private readonly int _value;
public S(int f)
{
_value = f;
}
public bool Equals(S other)
=> _value == other._value;
public override bool Equals(object obj)
=> obj is S objS && Equals(objS);
public override int GetHashCode()
=> _value.GetHashCode();
}
When to suppress warnings
Do not suppress violations of this rule.
See alsoCollaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, see our contributor guide. Additional resources In this articleWas this page helpful?
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4