Export (0) Print
Expand All
2 out of 5 rated this helpful - Rate this topic

Object.Equals Method (Object, Object)

Determines whether the specified Object instances are considered equal.

Namespace: System
Assembly: mscorlib (in mscorlib.dll)

public static bool Equals (
	Object objA,
	Object objB
)
public static boolean Equals (
	Object objA, 
	Object objB
)
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.

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).

The following code example compares different objects.

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

*/

import System.*;

public class MyClass
{
    public static void main(String[] args)
    {
        String s1 = "Tom";
        String s2 = "Carol";
        Console.WriteLine("Object.Equals(\"{0}\", \"{1}\") => {2}", s1, s2, 
            System.Convert.ToString(Object.Equals(s1, s2)));

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

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

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

        s1 = null;
        s2 = null;
        Console.WriteLine("Object.Equals(null, null) => {2}", s1, s2, 
            System.Convert.ToString(Object.Equals(s1, s2)));
    } //main
} //MyClass


/*

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

*/

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

*/

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.