Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
| Property | Value |
|---|---|
| Rule ID | CA1067 |
| Title | Override Equals when implementing IEquatable |
| Category | Design |
| Fix is breaking or non-breaking | Non-breaking |
| Enabled by default in .NET 10 | As suggestion |
| Applicable languages | C# and Visual Basic |
Cause
A type implements IEquatable
Rule description
A type implementing IEquatable
Your Equals implementation should return results that are consistent with System.IEquatable
How to fix violations
To fix a violation, override Equals and implement it by invoking the System.IEquatable
using System;
public struct S : IEquatable
{
private readonly int _value;
public S(int f)
{
_value = f;
}
public bool Equals(S other)
=> _value == other._value;
}
using System;
public struct S : IEquatable
{
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.