Senator Guerra Souty original series calendar,replica hublot blue steel peach pointer collocation of rolex replica Rome digital scale, track type minute replica watches scale shows that the classical model is swiss replica watches incomparable, wearing elegant dress highlights.


YOU ARE HERE: HOME Questions In the context of a comparison what is object identity versus object equivalence

Object Identity vs Object Equivalence

View(s): 11772

In the context of a comparison, what is object identity versus object equivalence?

Answer 1)

Generally speaking, two objects are identical if they share the same address in memory. This means that they are both referring to the same instance of a class. You can test if two objects are identical by using the ReferenceEquals static method of System.Object: Object.ReferenceEquals(myFirstObject, mySecondObject).

On the other hand, two objects are equivalent if they are instances of the same type and if every field of the first object matches the value of the same field of the second object. Being equivalent does not imply that the objects are identical: they can be independent instances, but their fields should have the same value. Because testing for equivalence can and probably will be different for most types, there is no global solution. For this purpose System.Object provides the virtual (overridable) method Equals to determine if two objects are equal.

The interesting thing is that if you don't override the virtual method Equals of System.Object when creating your own objects, the virtual method Equals looks like this:

class Object {
public virtual Boolean Equals(Object obj) {

// If both references point to the same
// object, they must be equal.
if (this == obj) return (true);

// Assume that the objects are not equal.
return (false);

So really, the virtual method Equals of System.Object only tests if the two objects are identical (share the same address in memory) not equivalent.  You will need to override this method if you want Equals to act any differently.

Did you ever think of System.String class. Being it is a reference type, instead of checking for same memory address (object identity), it checks for content (object equivalent). Right? But how? Microsoft did the same, they overrided the virtual method Equals of System.Object in the System.String class.

  Asked in:  SemanticSpace Technologies   Expertise Level:  Experienced
  Last updated on Tuesday, 20 September 2011
4/5 stars (2 vote(s))

Register Login Ask Us Write to Us Help