asp.net.ph

Object.Equals Method ( Object, Object )

System Namespace   Object Class


Determines whether the specified Object instances are considered equal.

[ VB ]
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 checks if both parameters are null references before calling objA.Equals ( objB ).

Example

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

[ C# ]
using System;

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

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

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

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

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


/*

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

*/
See Also

Object Members   Object.Equals Overload List   ReferenceEquals   ValueType Skip Navigation Links




Home
Suggested Reading


Previous page Back to top Next page

© 2000-2010 Rey Nuñez All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note

You can help support asp.net.ph