Export (0) Print
Expand All

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
Not applicable.

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.

Exception typeCondition

NullReferenceException

The obj parameter is a null reference (Nothing in Visual Basic).

The default implementation of Equals supports reference equality for reference types, and bitwise equality for value types. Reference equality means the object references that are compared refer to the same object. Bitwise equality means the objects that are compared have the same binary representation.

Note that a derived type might override the Equals method to implement value equality. Value equality means the compared objects have the same value but different binary representations.

For more information, see the Object.Equals(Object) topic.

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 Server 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 Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show:
© 2014 Microsoft