System Namespace Object Class
Determines whether the specified Object is equal to the current Object.
[ VB ]
Overridable Overloads Public Function Equals ( _
ByVal obj As Object _
) As Boolean
[ C# ]
public virtual bool Equals (
object obj
);
[ C++ ]
public: virtual bool Equals (
Object* obj
);
[ JScript ]
public function Equals (
obj : Object
) : Boolean;
- obj
- The Object to compare with the current Object.
true if the specified Object is equal to the current Object; otherwise, false.
The default implementation of Equals supports reference equality only, but derived classes can override this method to support value equality.
For reference types, equality is defined as object equality; that is, whether the references refer to the same object. For value types, equality is defined as bitwise equality. The ValueType class supports value types.
Notes to Implementers:
This method can be overridden by a derived class. For example, many of the base data types return true if both objects represent the same value; otherwise, false.
This method only compares primitives and objects. It must be overridden to compare more complex structures, such as arrays of objects.
The following statements must be true for all implementations of the Equals method. In the list, x, y, and z represent non-null object references.
- x.Equals ( x ) returns true, except in cases that involve floating-point types and NaNs. See IEC 60559:1989, Binary Floating-point Arithmetic for Microprocessor Systems.
- x.Equals ( y ) returns the same value as y.Equals ( x ).
- ( x.Equals ( y ) && y.Equals ( z ) ) returns true if and only if x.Equals ( z ) returns true.
- Successive calls to x.Equals ( y ) return the same value as long as the objects referenced by x and y are not modified.
- x.Equals ( a null reference ( Nothing in Visual Basic ) ) returns false.
See GetHashCode for additional required behaviors pertaining to the Equals method.
Implementations of Equals must not throw exceptions.
For some kinds of objects, it is desirable to have Equals test for value equality instead of referential equality. Such implementations of Equals return true if the two objects have the same "value", even if they are not the same instance. The type's implementer decides what constitutes an object's "value", but it is typically some or all the data stored in the instance variables of the object. For example, the value of a String is based on the characters of the string; the Equals method of the String class returns true for any two string instances that contain exactly the same characters in the same order.
Types that implement IComparable must override Equals.
Types that override Equals must also override GetHashCode; otherwise, Hashtable might not work correctly.
If your programming language supports operator overloading and if you choose to overload the equality operator for a given type, that type should override the Equals method. Such implementations of the Equals method should return the same results as the equality operator. Following this guideline will help ensure that class library code using Equals ( such as ArrayList and Hashtable ) behaves in a manner that is consistent with the way the equality operator is used by application code.
The following guidelines are for implementing a value type:
- Consider overriding Equals to gain increased performance over that provided by the default implementation of Equals on ValueType.
- If you override Equals and the language supports operator overloading, you must overload the equality operator for your value type.
The following guidelines are for implementing a reference type:
- Consider overriding Equals on a reference type if the semantics of the type are based on the fact that the type represents some value ( s ).
- Most reference types should not overload the equality operator, even if they override Equals. However, if you are implementing a reference type that is intended to have value semantics, such as a complex number type, you should override the equality operator.
The following code example compares the current instance with another object.
[ VB ]
Imports System
Public Class Sample
Sub Method ( )
Dim Obj1 As New Object ( )
Dim Obj2 As New Object ( )
Response.Write ( Obj1.Equals ( Obj2 ) ) '===> false
Obj2 = Obj1
Response.Write ( Obj1.Equals ( Obj2 ) ) '===> true
End Sub 'Method
End Class 'Sample
[ C# ]
using System;
public class Sample {
void Method ( ) {
Object Obj1 = new Object ( );
Object Obj2 = new Object ( );
Response.Write ( Obj1.Equals ( Obj2 ) ); //===> false
Obj2 = Obj1;
Response.Write ( Obj1.Equals ( Obj2 ) ); //===> true
}
}
[ C++ ]
#using <mscorlib.dll>
#using <System.DLL>
using namespace System;
__gc class Sample
{
void Method ( )
{
Object *Obj1 = new Object ( );
Object *Obj2 = new Object ( );
Console::WriteLine ( Obj1->Equals ( Obj2 ) ); //===> false
Obj2 = Obj1;
Console::WriteLine ( Obj1->Equals ( Obj2 ) ); //===> true
}
};
[ VB, C#, C++ ] The following example shows a Point class that overrides the Equals method to provide value equality and a class Point3D, which is derived from Point. Because Point 's override of Equals is the first in the inheritance chain to introduce value equality, the Equals method of the base class ( which is inherited from Object and checks for referential equality ) is not invoked. However, Point3D.Equals invokes Point.Equals because Point implements Equals in a manner that provides value equality.
[ C# ]
using System;
class Point: object {
int x, y;
public override bool Equals ( Object obj ) {
//Check for null and compare run-time types.
if ( obj == null || GetType ( ) != obj.GetType ( ) ) return false;
Point p = ( Point ) obj;
return ( x == p.x ) && ( y == p.y );
}
public override int GetHashCode ( ) {
return x ^ y;
}
}
class Point3D: Point {
int z;
public override bool Equals ( Object obj ) {
return base.Equals ( obj ) && z == ( ( Point3D ) obj ).z;
}
public override int GetHashCode ( ) {
return base.GetHashCode ( ) ^ z;
}
}
[ VB, C#, C++ ] The Point.Equals method checks that the obj argument is not a null reference ( Nothing in Visual Basic ) and that it references an instance of the same type as this object. If either of those checks fail, the method returns false.
[ VB, C#, C++ ] The Equals method uses GetType to determine whether the run-time types of the two objects are identical. ( Note that typeof is not used here because it returns the static type. ) If the method used a check of the form obj is Point
, the check would return true in cases where obj is an instance of a derived class of Point, even though obj and the current instance are not of the same runtime type. Having verified that both objects are of the same type, the method casts obj to type Point and returns the result of comparing the instance variables of the two objects.
[ VB, C#, C++ ] In Point3D.Equals, the inherited Equals method is invoked before anything else is done; the inherited Equals method checks to see that obj is not a null reference ( Nothing ), that obj is an instance of the same class as this object and that the inherited instance variables match. Only when the inherited Equals returns true does the method compare the instance variables introduced in the derived class. Specifically, the cast to Point3D is not executed unless obj has been determined to be of type Point3D or a derived class of Point3D.
[ VB, C#, C++ ] In the previous example, operator == ( the equality operator ) is used to compare the individual instance variables. In some cases, it is appropriate to use the Equals method to compare instance variables in an Equals implementation, as shown in the following code example.
[ C# ]
using System;
class Rectangle {
Point a, b;
public override bool Equals ( Object obj ) {
if ( obj == null || GetType ( ) != obj.GetType ( ) ) return false;
Rectangle r = ( Rectangle ) obj;
//Use Equals to compare instance variables
return a.Equals ( r.a ) && b.Equals ( r.b );
}
public override int GetHashCode ( ) {
return a.GetHashCode ( ) ^ b.GetHashCode ( );
}
}
[ VB, C#, C++ ] In some languages, such as C#, operator overloading is supported. When a type overloads operator ==, it should also override the Equals method to provide the same functionality. This is typically accomplished by writing the Equals method in terms of the overloaded operator ==, as in the following code example.
[ C# ]
using System;
public struct Complex {
double re, im;
public override bool Equals ( Object obj ) {
return obj is Complex && this == ( Complex ) obj;
}
public override int GetHashCode ( ) {
return re.GetHashCode ( ) ^ im.GetHashCode ( );
}
public static bool operator == ( Complex x, Complex y ) {
return x.re == y.re && x.im == y.im;
}
public static bool operator != ( Complex x, Complex y ) {
return ! ( x == y );
}
}
[ VB, C#, C++ ] Because Complex is a C# struct ( a value type ), it cannot be derived from; therefore, the Equals method need not compare the GetType results for each object, but can instead use the is operator to check the type of the obj parameter.
Object Members Object.Equals Overload List ReferenceEquals GetHashCode ValueType IComparable System.Collections.ArrayList System.Collections.Hashtable