Export (0) Print
Expand All

Object.Equals Method (Object, Object)

.NET Framework 1.1

Determines whether the specified Object instances are considered equal.

[Visual Basic]
Overloads Public Shared Function Equals( _
   ByVal objA As Object, _
   ByVal objB As Object _
) As Boolean
[C#]
public static bool Equals(
 object objA,
 object objB
);
[C++]
public: static bool Equals(
 Object* objA,
 Object* objB
);
[JScript]
public static function Equals(
   objA : Object,
 objB : Object
) : Boolean;

Parameters

objA
The first Object to compare.
objB
The second Object to compare.

Return Value

true if objA is the same instance as objB or if both are null references or if objA.Equals(objB) returns true; otherwise, false.

Remarks

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.

This method first determines whether both parameters are null references before calling objA.Equals(objB).

Example

[C#, C++, JScript] The following code example compares different objects.

[C#] 
using System;

public class MyClass {
   public static void Main() {
   string s1 = "Tom";
   string s2 = "Carol";
   Console.WriteLine("Object.Equals(\"{0}\", \"{1}\") => {2}", 
      s1, s2, Object.Equals(s1, s2));

   s1 = "Tom";
   s2 = "Tom";
   Console.WriteLine("Object.Equals(\"{0}\", \"{1}\") => {2}", 
      s1, s2, Object.Equals(s1, s2));

   s1 = null;
   s2 = "Tom";
   Console.WriteLine("Object.Equals(null, \"{1}\") => {2}",
       s1, s2, Object.Equals(s1, s2));

   s1 = "Carol";
   s2 = null;
   Console.WriteLine("Object.Equals(\"{0}\", null) => {2}", 
       s1, s2, Object.Equals(s1, s2));

   s1 = null;
   s2 = null;
   Console.WriteLine("Object.Equals(null, null) => {2}", 
       s1, s2, Object.Equals(s1, s2));
   }
}


/*

This code produces the following output.

Object.Equals("Tom", "Carol") => False
Object.Equals("Tom", "Tom") => True
Object.Equals(null, "Tom") => False
Object.Equals("Carol", null) => False
Object.Equals(null, null) => True

*/

[C++] 
#using <mscorlib.dll>
using namespace System;

int main() {
   String* s1 = S"Tom";
   String* s2 = S"Carol";
   Console::WriteLine(S"Object.Equals(\"{0}\", \"{1}\") => {2}",
      s1, s2, __box(Object::Equals(s1,s2)));

   s1 = S"Tom";
   s2 = S"Tom";
   Console::WriteLine(S"Object.Equals(\"{0}\", \"{1}\") => {2}",
      s1, s2, __box(Object::Equals(s1,s2)));

   s1 = 0;
   s2 = S"Tom";
   Console::WriteLine(S"Object.Equals(null, \"{1}\") => {2}",
      s1, s2, __box(Object::Equals(s1,s2)));

   s1 = S"Carol";
   s2 = 0;
   Console::WriteLine(S"Object.Equals(\"{0}\", null) => {2}",
      s1, s2, __box(Object::Equals(s1,s2)));

   s1 = 0;
   s2 = 0;
   Console::WriteLine(S"Object.Equals(null, null) => {2}",
      s1, s2, __box(Object::Equals(s1,s2)));
}


/*

This code produces the following output.

Object.Equals("Tom", "Carol") => False
Object.Equals("Tom", "Tom") => True
Object.Equals(null, "Tom") => False
Object.Equals("Carol", null) => False
Object.Equals(null, null) => True

*/

[JScript] 
import System

package Equals0
{    

public class MyClass {
   public static function Main() {
   var s1 : String = "Tom";
   var s2 : String = "Carol";
   var array : Object [] = new Object[3];

   array[0] = s1;
   array[1] = s2;
   array[2] = System.Object.Equals(s1, s2);

   Console.WriteLine("Object.Equals('{0}', '{1}') => {2}", 
      array);

   s1 = "Tom";
   s2 = "Tom";
   array[0] = s1;
   array[1] = s2;
   array[2] = System.Object.Equals(s1, s2);
   
   Console.WriteLine("Object.Equals('{0}', '{1}') => {2}", 
      array);

   s1 = null;
   s2 = "Tom";
   array[0] = s1;
   array[1] = s2;
   array[2] = System.Object.Equals(s1, s2);
   
   Console.WriteLine("Object.Equals(null, '{1}') => {2}",
       array);

   s1 = "Carol";
   s2 = null;
   array[0] = s1;
   array[1] = s2;
   array[2] = System.Object.Equals(s1, s2);
   
   Console.WriteLine("Object.Equals('{0}', null) => {2}", 
       array);

   s1 = null;
   s2 = null;
   array[0] = s1;
   array[1] = s2;
   array[2] = System.Object.Equals(s1, s2);
   Console.WriteLine("Object.Equals(null, null) => {2}", 
       array);
   }   
}

}

Equals0.MyClass.Main();


/*

This code produces the following output.

Object.Equals("Tom", "Carol") => False
Object.Equals("Tom", "Tom") => True
Object.Equals(null, "Tom") => False
Object.Equals("Carol", null) => False
Object.Equals(null, null) => True

*/

[Visual Basic] No example is available for Visual Basic. To view a C#, C++, or JScript example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

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

See Also

Object Class | Object Members | System Namespace | Object.Equals Overload List | ReferenceEquals | ValueType

Show:
© 2014 Microsoft