Equals Method

Object.Equals Method

Determines whether two Object instances are equal.

Overload List

Determines whether the specified Object is equal to the current Object.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Overridable Function Equals(Object) As Boolean
[C#] public virtual bool Equals(object);
[C++] public: virtual bool Equals(Object*);
[JScript] public function Equals(Object) : Boolean;

Determines whether the specified Object instances are considered equal.

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

Example

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

[C#, C++, JScript] Note   This example shows how to use one of the overloaded versions of Equals. For other examples that might be available, see the individual overload topics.
[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.

See Also

Object Class | Object Members | System Namespace

Show:
© 2016 Microsoft