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

Code analysis

CA2224: Override Equals() on overloading operator equals

Learn about code analysis rule CA2224: Override Equals() on overloading operator equals

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

Cause

A public type implements the equality operator but doesn't override the System.Object.Equals method.

Rule description

The equality operator is intended to be a syntactically convenient way to access the functionality of the System.Object.Equals method. If you implement the equality operator, its logic must be identical to that of System.Object.Equals.

How to fix violations

To fix a violation of this rule, you should either remove the implementation of the equality operator, or override System.Object.Equals and have the two methods return the same values. If the equality operator doesn't introduce inconsistent behavior, you can fix the violation by providing an implementation of System.Object.Equals that calls the System.Object.Equals method in the base class.

When to suppress warnings

It's safe to suppress a warning from this rule if the equality operator returns the same value as the inherited implementation of System.Object.Equals. The examples in this article include a type that could safely suppress a warning from this rule.

Suppress a warning

If you just want to suppress a single violation, add preprocessor directives to your source file to disable and then re-enable the rule.

csharp
#pragma warning disable CA2224
// The code that's violating the rule is on this line.
#pragma warning restore CA2224

To disable the rule for a file, folder, or project, set its severity to none in the configuration file.

ini
[*.{cs,vb}]
dotnet_diagnostic.CA2224.severity = none

For more information, see How to suppress code analysis warnings.

Example

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

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

vb
Imports System
Imports System.IO

Namespace ca2224
    '<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 GetHashCode() As Integer
            Return HashCode.Combine(X, Y)
        End Function

        Public Shared Operator =(pt1 As Point, pt2 As Point) As Boolean
            If pt1 Is Nothing OrElse pt2 Is Nothing Then
                Return False
            End If

            If pt1.GetType() <> pt2.GetType() Then
                Return False
            End If

            Return pt1.X = pt2.X AndAlso pt1.Y = pt2.Y
        End Operator

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

    End Class
    '</snippet1>
End Namespace

Namespace ca2224_2
    '<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 HashCode.Combine(X, 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 X = pt.X AndAlso Y = pt.Y

        End Function

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

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

    End Class
    '</snippet2>
End Namespace

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

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

vb
Imports System
Imports System.IO

Namespace ca2224
    '<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 GetHashCode() As Integer
            Return HashCode.Combine(X, Y)
        End Function

        Public Shared Operator =(pt1 As Point, pt2 As Point) As Boolean
            If pt1 Is Nothing OrElse pt2 Is Nothing Then
                Return False
            End If

            If pt1.GetType() <> pt2.GetType() Then
                Return False
            End If

            Return pt1.X = pt2.X AndAlso pt1.Y = pt2.Y
        End Operator

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

    End Class
    '</snippet1>
End Namespace

Namespace ca2224_2
    '<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 HashCode.Combine(X, 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 X = pt.X AndAlso Y = pt.Y

        End Function

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

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

    End Class
    '</snippet2>
End Namespace

See also