Muokkaa

Jaa


CA2224: Override Equals on overloading operator equals

Property Value
Rule ID CA2224
Title Override Equals on overloading operator equals
Category Usage
Fix is breaking or non-breaking Non-breaking
Enabled by default in .NET 9 As suggestion

Cause

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

Rule description

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

Note

This rule only applies to Visual Basic code. The C# compiler generates a separate warning, CS0660.

How to fix violations

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

When to suppress warnings

It is safe to suppress a warning from this rule if the equality operator returns the same value as the inherited implementation of 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.

#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.

[*.{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.

' 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

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

' 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

See also