Object::Equals Method (Object^)
Determines whether the specified object is equal to the current object.
Assembly: mscorlib (in mscorlib.dll)
The type of comparison between the current instance and the obj parameter depends on whether the current instance is a reference type or a value type.
If the current instance is a reference type, the ReferenceEquals method. Reference equality means that the object variables that are compared refer to the same object. The following example illustrates the result of such a comparison. It defines a Person class, which is a reference type, and calls the Person class constructor to instantiate two new Person objects, person1a and person2, which have the same value. It also assigns person1a to another object variable, person1b. As the output from the example shows, person1a and person1b are equal because they reference the same object. However, person1a and person2 are not equal, although they have the same value.method tests for reference equality, and a call to the method is equivalent to a call to the
If the current instance is a value type, themethod tests for value equality. Value equality means the following:
The two objects are of the same type. As the following example shows, a Byte object that has a value of 12 does not equal an Int32 object that has a value of 12, because the two objects have different run-time types.
The values of the public and private fields of the two objects are equal. The following example tests for value equality. It defines a Person structure, which is a value type, and calls the Person class constructor to instantiate two new Person objects, person1 and person2, which have the same value. As the output from the example shows, although the two object variables refer to different objects, person1 and person2 are equal because they have the same value for the private personName field.
Because the Object class is the base class for all types in the .NET Framework, the method provides the default equality comparison for all other types. However, types often override the method to implement value equality. For more information, see the Notes for Callers and Notes for Inheritors sections.
When you call the .NET Framework Support for Windows Store Apps and Windows Runtime). Classes in the Windows Runtime don’t inherit Object, and currently don’t implement an method. However, they appear to have ToString, , and GetHashCode methods when you use them in your C# or Visual Basic code, and the .NET Framework provides the default behavior for these methods.method overload on a class in the Windows Runtime, it provides the default behavior for classes that don’t override . This is part of the support that the .NET Framework provides for the Windows Runtime (see
Windows Runtime classes that are written in C# or Visual Basic can override themethod overload.
Derived classes frequently override the IEquatable<T> interface. When you call the Equals method to test for equality, you should know whether the current instance overrides and understand how a particular call to an Equals method is resolved. Otherwise, you may be performing a test for equality that is different from what you intended, and the method may return an unexpected value.method to implement value equality. In addition, types also frequently provide an additional strongly typed overload to the Equals method, typically by implementing the
The following example provides an illustration. It instantiates three StringBuilder objects with identical strings, and then makes four calls to Equals methods. The first method call returns true, and the remaining three return false.
In the first case, the strongly typed StringBuilder::Equals(StringBuilder^) method overload, which tests for value equality, is called. Because the strings assigned to the two StringBuilder objects are equal, the method returns true. However, StringBuilder does not override . Because of this, when the StringBuilder object is cast to an Object, when a StringBuilder instance is assigned to a variable of type Object, and when the Object::Equals(Object^, Object^) method is passed two StringBuilder objects, the default method is called. Because StringBuilder is a reference type, this is equivalent to passing the two StringBuilder objects to the ReferenceEquals method. Although all three StringBuilder objects contain identical strings, they refer to three distinct objects. As a result, these three method calls return false.
You can compare the current object to another object for reference equality by calling the ReferenceEquals method. In Visual Basic, you can also use the is keyword (for example, If Me Is otherObject Then ...).
When you define your own type, that type inherits the functionality defined by the Equals method of its base type. The following table lists the default implementation of the Equals method for the major categories of types in the .NET Framework.
Equality defined by
Class derived directly from Object
Reference equality; equivalent to calling Object::ReferenceEquals.
Value equality; either direct byte-by-byte comparison or field-by-field comparison using reflection.
Values must have the same enumeration type and the same underlying value.
Delegates must have the same type with identical invocation lists.
For a value type, you should always override String object is based on the characters of the string; the String::Equals(Object^) method overrides the method to return true for any two string instances that contain the same characters in the same order., because tests for equality that rely on reflection offer poor performance. You can also override the default implementation of for reference types to test for value equality instead of reference equality and to define the precise meaning of value equality. Such implementations of 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
The following example shows how to override the Person class. If Person accepted its base class implementation of equality, two Person objects would be equal only if they referenced a single object. However, in this case, two Person objects are equal if they have the same value for the Person.Id property.method to test for value equality. It overrides the method for the
In addition to overriding IEquatable<T> interface to provide a strongly typed test for equality., you can implement the
The following statements must be true for all implementations of the x, y, and z represent object references that are not null.method. In the list,
x.Equals(x) returns true, except in cases that involve floating-point types. See ISO/IEC/IEEE 60559:2011, Information technology -- Microprocessor Systems -- Floating-Point arithmetic.
x.Equals(y) returns the same value as y.Equals(x).
x.Equals(y) returns true if both x and y are NaN.
If (x.Equals(y) && y.Equals(z)) returns true, then 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(null) returns false.
Implementations of ArgumentNullException.must not throw exceptions; they should always return a value. For example, if obj is null, the method should return false instead of throwing an
Follow these guidelines when overriding:
Types that implement IComparable must override .
Types that override GetHashCode; otherwise, hash tables might not work correctly.must also override
If your programming language supports operator overloading and you overload the equality operator for a given type, you must also override the ArrayList and Hashtable) behaves in a manner that is consistent with the way the equality operator is used by application code.method to return the same result as the equality operator. This helps ensure that class library code that uses (such as
The following guidelines apply to overridingfor a reference type:
Consider overridingif the semantics of the type are based on the fact that the type represents some value(s).
Most reference types must not overload the equality operator, even if they override. However, if you are implementing a reference type that is intended to have value semantics, such as a complex number type, you must override the equality operator.
You should not override GetHashCode method, as discussed in the previous section. This means that the hash code of an instance of a mutable reference type can change during its lifetime, which can cause the object to be lost in a hash table.on a mutable reference type. This is because overriding requires that you also override the
The following guidelines apply to overridingfor a value type:
If you are defining a value type that includes one or more fields whose values are reference types, you should override ValueType performs a byte-by-byte comparison for value types whose fields are all value types, but it uses reflection to perform a field-by-field comparison of value types whose fields include reference types.. The implementation provided by
If you overrideand your development language supports operator overloading, you must overload the equality operator.
The following example shows a Point class that overrides the method to provide value equality, and a Point3D class that is derived from Point. Because Point overrides to test for value equality, the method is not called. However, Point3D.Equals calls Point.Equals because Point implements in a manner that provides value equality.
The following example defines a Rectangle class that internally implements a rectangle as two Point objects. The Rectangle class also overrides to provide for value equality.
Some languages such as C# and Visual Basic support operator overloading. When a type overloads the equality operator, it must also override themethod to provide the same functionality. This is typically accomplished by writing the method in terms of the overloaded equality operator, as in the following example.
Available since 4.5
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Available since 8.1