The requested page is not available for the requested platform. You are viewing the content for Default platform.

Implement IEquatable

  • 2 min read

Implements the IEquatable interface in the current class.

Availability

From the context menus or via shortcuts:

  • when the edit cursor or caret is on a declaration of a class containing fields or properties, provided that the class does not implement IEquatable.

Example

public class MyClass
{
  public string StringValue { get; set; }
  public string IntValue { get; set; }
}
Public Class TestClass
  Public Property StringValue() As String
  Public Property IntValue() As String
End Class

Result:

public class MyClass : IEquatable<MyClass>
{
  public override bool Equals(object obj)
  {
    if(obj is MyClass)
      return Equals((MyClass)obj);
    return base.Equals(obj);
  }
  public static bool operator ==(MyClass first, MyClass second)
  {
    if((object)first == null)
      return (object)second == null;
    return first.Equals(second);
  }
  public static bool operator !=(MyClass first, MyClass second)
  {
    return !(first == second);
  }
  public bool Equals(MyClass other)
  {
    if(ReferenceEquals(null, other))
      return false;
    if(ReferenceEquals(this, other))
      return true;
    return Equals(this.StringValue, other.StringValue) && Equals(this.IntValue, other.IntValue);
  }
  public override int GetHashCode()
  {
    unchecked {
      int hashCode = 47;
      if(this.StringValue != null)
        hashCode = (hashCode * 53) ^ this.StringValue.GetHashCode();
      if(this.IntValue != null)
        hashCode = (hashCode * 53) ^ this.IntValue.GetHashCode();
      return hashCode;
    }
  }

  public string StringValue { get; set; }
  public string IntValue { get; set; }
}
Public Class TestClass
  Implements IEquatable(Of TestClass)
  Public Overrides Function Equals(ByVal obj As Object) As Boolean
    If TypeOf obj Is TestClass Then
      Return Equals(CType(obj, TestClass))
    End If
    Return MyBase.Equals(obj)
  End Function
  Public Shared Operator =(ByVal first As TestClass, ByVal second As TestClass) 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 <>(ByVal first As TestClass, ByVal second As TestClass) As Boolean
    Return Not (first = second)
  End Operator
  Public Overloads Function Equals(ByVal other As TestClass) As Boolean Implements IEquatable(Of TestClass).Equals
    If ReferenceEquals(Nothing, other) Then
      Return False
    End If
    If ReferenceEquals(Me, other) Then
      Return True
    End If
    Return Equals(Me.StringValue, other.StringValue) AndAlso Equals(Me.IntValue, other.IntValue)
  End Function
  Public Overrides Function GetHashCode() As Integer
    Dim hashCode As Long = 47
    If Me.StringValue IsNot Nothing Then
      hashCode = CInt((hashCode * 53 + Me.StringValue.GetHashCode()) And &H7FFFFFFFL)
    End If
    If Me.IntValue IsNot Nothing Then
      hashCode = CInt((hashCode * 53 + Me.IntValue.GetHashCode()) And &H7FFFFFFFL)
    End If
    Return CInt(hashCode)
  End Function
  Public Property StringValue() As String
  Public Property IntValue() As String
End Class