Hendelser
17. mars, 21 - 21. mars, 10
Bli med i meetup-serien for å bygge skalerbare AI-løsninger basert på virkelige brukstilfeller med andre utviklere og eksperter.
Registrer deg nåDenne nettleseren støttes ikke lenger.
Oppgrader til Microsoft Edge for å dra nytte av de nyeste funksjonene, sikkerhetsoppdateringene og den nyeste tekniske støtten.
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 9 | As suggestion |
A type implements IEquatable<T>, but does not override Equals method.
A 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. See here for more details.
Your Equals implementation should return results that are consistent with System.IEquatable<T>.Equals implementation.
To 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();
}
Do not suppress violations of this rule.
.NET-tilbakemelding
.NET er et åpen kilde-prosjekt. Velg en kobling for å gi tilbakemelding:
Hendelser
17. mars, 21 - 21. mars, 10
Bli med i meetup-serien for å bygge skalerbare AI-løsninger basert på virkelige brukstilfeller med andre utviklere og eksperter.
Registrer deg nå