다음을 통해 공유


ValueType.Equals Method

Indicates whether this instance and a specified object are equal.

Overrides Public Function Equals( _
   ByVal obj As Object _) As Boolean
[C#]
public override bool Equals(objectobj);
[C++]
public: bool Equals(Object* obj);
[JScript]
public override function Equals(
   obj : Object) : Boolean;

Parameters

  • obj
    Another object to compare to.

Return Value

true if obj and this instance are the same type and represent the same value; otherwise, false.

Remarks

The default implementation of the Equals method uses reflection to compare the corresponding fields of obj and this instance. Override the Equals method for a particular type to improve the performance of the method and more closely represent the concept of equality for the type.

Example

The following example demonstrates how the Equals method can be overridden by a derived value type.

 
Public Structure Complex
   Private m_Re As Double
   Private m_Im As Double
       
   Public Overloads Function Equals(ob As Object) As Boolean
      If TypeOf ob Is Complex Then
         Dim c As Complex = CType(ob, Complex)
         Return m_Re = c.m_Re And m_Im = c.m_Im
      Else
         Return False
      End If
   End Function
   
   
   Public Overloads Function GetHashCode() As Integer
      Return m_Re.GetHashCode() ^ m_Im.GetHashCode()
   End Function

End Structure

[C#] 
public struct Complex 
{
    public double m_Re;
    public double m_Im;

    public override bool Equals( object ob ){
        if( ob is Complex ) {
            Complex c = (Complex) ob;
            return m_Re==c.m_Re && m_Im==c.m_Im;
        }
        else {
            return false;
        }
    }

    public override int GetHashCode(){
        return m_Re.GetHashCode() ^ m_Im.GetHashCode();
    }
}

[C++] 
public __gc struct Complex {
   double  m_Re;
   double  m_Im;
   bool Equals(Object* ob) {
      if (dynamic_cast<Complex*>(ob)) {
         Complex* c = dynamic_cast<Complex*>(ob);
         return m_Re==c->m_Re && m_Im==c->m_Im;
      } else {
         return false;
      }
   }
   int GetHashCode() {
      return  __box(m_Re)->GetHashCode() ^  __box(m_Im)->GetHashCode();
   }
};

[JScript] 
public class Complex 
{
    public var m_Re : double;
    public var m_Im : double;

    public override function Equals( ob ) : Boolean{
        if( ob.GetType() == Complex ) {
            var c : Complex = Complex(ob);
            return m_Re==c.m_Re && m_Im==c.m_Im;
        }
        else {
            return false;
        }
    }

    public override function GetHashCode() : int{
        return m_Re.GetHashCode() ^ m_Im.GetHashCode();
    }
            
            public static function main() {
                   var x : Complex = new Complex();
                   x.m_Re = 1;
                   x.m_Im = 2;
                   var y : Complex = new Complex();
                   y.m_Re = 2;
                   y.m_Im = 1;
                   
                   Console.Write(x.Equals(y));                       
            }
}

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

ValueType Class | ValueType Members | System Namespace