Back to Devexpress

Implement IEquatable

coderushforroslyn-115533-coding-assistance-code-providers-implementation-providers-implement-iequatable.md

latest4.6 KB
Original Source

Implement IEquatable

  • Aug 03, 2020
  • 4 minutes to read

Purpose

Using this Code Provider , you can easily add the implementation of the IEquatable<T> interface to any class containing at least one field or property. This Code Provider adds the IEquatable<T> implementation along with all required overloads (two overloads of the Equals method, GetHashCode method, == ( = in VB) operator and != ( <> in VB) operator) to the class.

Availability

Available when the caret is on a class declaration.

Usage

  1. Place the caret on a class declaration. The class must contain at least one field or property.

  2. Press the Ctrl + . or Ctrl + ~ shortcut to invoke the Code Actions menu.

  3. Select Implement IEquatable from the menu.

After execution, the Code Provider adds the implementation of the IEquatable<T> interface. It also adds the corresponding overloads of methods and operators with their common implementation.

csharp
public class Temperature : IEquatable<Temperature> {
    protected double temperatureF;
    public double Fahrenheit {
        get {
            return this.temperatureF;
        }
        set {
            this.temperatureF = value;
        }
    }
    public double Celsius {
        get {
            return (this.temperatureF - 32) * (5.0 / 9);
        }
        set {
            this.temperatureF = (value * 9.0 / 5) + 32;
        }
    }

    public override bool Equals(object obj) {
        if (obj is Temperature)
            return Equals((Temperature)obj);
        return base.Equals(obj);
    }

    public static bool operator ==(Temperature first, Temperature second) {
        if ((object)first == null)
            return (object)second == null;
        return first.Equals(second);
    }

    public static bool operator !=(Temperature first, Temperature second) {
        return !(first == second);
    }

    public bool Equals(Temperature other) {
        if (ReferenceEquals(null, other))
            return false;
        if (ReferenceEquals(this, other))
            return true;
        return this.temperatureF.Equals(other.temperatureF);
    }

    public override int GetHashCode() {
        unchecked {
            int hashCode = 47;
            hashCode = (hashCode * 53) ^ temperatureF.GetHashCode();
            return hashCode;
        }
    }
}
vb
Public Class Temperature
    Implements IEquatable(Of Temperature)

    Protected temperatureF As Double
    Public Property Fahrenheit() As Double
        Get
            Return Me.temperatureF
        End Get
        Set(ByVal value As Double)
            Me.temperatureF = value
        End Set
    End Property
    Public Property Celsius() As Double
        Get
            Return (Me.temperatureF - 32) * (5.0 \ 9)
        End Get
        Set(ByVal value As Double)
            Me.temperatureF = (value * 9.0 / 5) + 32
        End Set
    End Property

    Public Overrides Function Equals(obj As Object) As Boolean
        If TypeOf obj Is Temperature Then
            Return Equals(CType(obj, Temperature))
        End If

        Return MyBase.Equals(obj)
    End Function

    Public Shared Operator =(first As Temperature, second As Temperature) As Boolean
        If CType(first, Object) Is Nothing Then
            Return CType(second, Object) Is Nothing
        End If

        Return first.Equals(second)
    End Operator

    Public Shared Operator <>(first As Temperature, second As Temperature) As Boolean
        Return Not (first = second)
    End Operator

    Public Overloads Function Equals(other As Temperature) As Boolean _
            Implements IEquatable(Of Temperature).Equals
        If ReferenceEquals(Nothing, other) Then
            Return False
        End If

        If ReferenceEquals(Me, other) Then
            Return True
        End If

        Return Me.temperatureF.Equals(other.temperatureF)
    End Function

    Public Overrides Function GetHashCode() As Integer
        Dim hashCode As Long = 47
        hashCode = CInt((hashCode * 53 + temperatureF.GetHashCode()) And &H7FFFFFFFL)
        Return CInt(hashCode)
    End Function
End Class

See Also

Implement ISerializable

Implement IComparable