Tips on System.Object

1) Whenever you override the Equals() method, you should also override the GetHashCode() method. If you don’t, you may find that the hash codes (unique numbers that identify objects) are unequal

2) There are two implementations of the Equals() method in the Object class. Their syntax is as follows: USe any 1 of them depending on choice:

public virtual bool Equals(object obj)
public virtual bool Equals(object objA, object objB)

In the comparison line (a == a1), we are testing the equality of the reference variables, which are identical. However, the intent of the second test line (a.Equals(a2)) is to test the data of the two objects for equality.

Animal a = new Animal();
Animal a1 = a;
Animal a2 = new Animal();
if (a == a1) // the equals operator compares the reference variables
System.Console.WriteLine (“The references are equal”);
if (a.Equals(a2)) // Compares Objects
System.Console.WriteLine (“The objects are equal”);
System.Console.WriteLine (“The objects are not equal”);

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s