CA2218: Eseguire l'override di GetHashCode all'override di Equals

Proprietà valore
ID regola CA2218
Title Eseguire l'override di GetHashCode all'override di Equals
Categoria Utilizzo
Correzione che causa un'interruzione o un'interruzione Nessuna interruzione
Abilitato per impostazione predefinita in .NET 8 Come suggerimento

Causa

Un tipo pubblico esegue l'override System.Object.Equals di ma non esegue l'override System.Object.GetHashCodedi .

Descrizione regola

GetHashCode restituisce un valore, in base all'istanza corrente, adatto per algoritmi hash e strutture di dati, ad esempio tabelle hash. Due oggetti che sono dello stesso tipo e sono uguali devono restituire lo stesso codice hash per garantire che le istanze dei tipi seguenti funzionino correttamente:

Nota

Questa regola si applica solo al codice Visual Basic. Il compilatore C# genera un avviso separato, CS0659.

Come correggere le violazioni

Per correggere una violazione di questa regola, fornire un'implementazione di GetHashCode. Per una coppia di oggetti dello stesso tipo, assicurarsi che l'implementazione restituisca lo stesso valore se l'implementazione di Equals restituisce true per la coppia.

Quando eliminare gli avvisi

Non escludere un avviso da questa regola.

Esempio di classe

Nell'esempio seguente viene illustrata una classe (tipo riferimento) che viola questa regola.

' This class violates the rule.
Public Class Point

    Public Property X As Integer
    Public Property Y As Integer

    Public Sub New(x As Integer, y As Integer)
        Me.X = x
        Me.Y = y
    End Sub

    Public Overrides Function Equals(obj As Object) As Boolean

        If obj = Nothing Then
            Return False
        End If

        If [GetType]() <> obj.GetType() Then
            Return False
        End If

        Dim pt As Point = CType(obj, Point)

        Return X = pt.X AndAlso Y = pt.Y

    End Function

End Class

Nell'esempio seguente viene risolta la violazione eseguendo l'override di GetHashCode().

' This class satisfies the rule.
Public Class Point

    Public Property X As Integer
    Public Property Y As Integer

    Public Sub New(x As Integer, y As Integer)
        Me.X = x
        Me.Y = y
    End Sub

    Public Overrides Function GetHashCode() As Integer
        Return X Or Y
    End Function

    Public Overrides Function Equals(obj As Object) As Boolean

        If obj = Nothing Then
            Return False
        End If

        If [GetType]() <> obj.GetType() Then
            Return False
        End If

        Dim pt As Point = CType(obj, Point)

        Return Equals(pt)

    End Function

    Public Overloads Function Equals(pt As Point) As Boolean
        Return X = pt.X AndAlso Y = pt.Y
    End Function

    Public Shared Operator =(pt1 As Point, pt2 As Point) As Boolean
        Return pt1.Equals(pt2)
    End Operator

    Public Shared Operator <>(pt1 As Point, pt2 As Point) As Boolean
        Return Not pt1.Equals(pt2)
    End Operator

End Class

Vedi anche