C#idiomatic C#
ca2218fundamentals/code-analysis/quality-rules

Code analysis

CA2218: Override GetHashCode on overriding Equals

Learn about code analysis rule CA2218: Override GetHashCode on overriding Equals

CA2218 Usage enabled by default in .NET 10: As suggestion
Fix is breaking or non-breaking Non-breakingApplicable languages Visual Basic

Cause

A public type overrides System.Object.Equals but does not override System.Object.GetHashCode.

Rule description

System.Object.GetHashCode returns a value, based on the current instance, that's suited for hashing algorithms and data structures such as hash tables. Two objects that are the same type and are equal must return the same hash code to ensure that instances of the following types work correctly:

How to fix violations

To fix a violation of this rule, provide an implementation of System.Object.GetHashCode. For a pair of objects of the same type, ensure that the implementation returns the same value if your implementation of System.Object.Equals returns true for the pair.

When to suppress warnings

Do not suppress a warning from this rule.

Class example

The following example shows a class (reference type) that violates this rule.

sample · vbdocs/fundamentals/code-analysis/quality-rules/snippets/vb/all-rules/ca2218.vb1

vb
Namespace ca2218
    '<snippet1>
    ' 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
    '</snippet1>
End Namespace

Namespace ca2218_fix
    '<snippet2>
    ' 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
    '</snippet2>
End Namespace

The following example fixes the violation by overriding System.Object.GetHashCode.

sample · vbdocs/fundamentals/code-analysis/quality-rules/snippets/vb/all-rules/ca2218.vb2

vb
Namespace ca2218
    '<snippet1>
    ' 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
    '</snippet1>
End Namespace

Namespace ca2218_fix
    '<snippet2>
    ' 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
    '</snippet2>
End Namespace

See also