If A.equals(B) is true then A.getHashcode & B.getHashCode must always return same hash code

A) True
B) False

Showing Answers 1 - 48 of 48 Answers

The answer is False because it is given that A.equals(B) returns true i.e. objects are equal and now its hashCode is asked which is always independent of the fact that whether objects are equal or not. So, GetHashCode for both of the objects returns different value.

Ans: False

  Was this answer useful?  Yes

visitor

  • Jan 19th, 2006
 

sameeksha is right.Answer is A. True.

  Was this answer useful?  Yes

imqwer

  • Jan 24th, 2006
 

True.

A.equals(B) will return true only and only if the hash codes for A and B are the same.

  Was this answer useful?  Yes

Prakash Nayak

  • Feb 23rd, 2006
 

The answer is A true if getHashcode is equal then only A.Equals B

  Was this answer useful?  Yes

apanmittal

  • Jun 6th, 2008
 

If two objects compare as equal, the GetHashCode method for each object must return the same value. However, if two objects do not compare as equal, the GetHashCode methods for the two object do not have to return different values. The GetHashCode method for an object must consistently return the same hash code as long as there is no modification to the object state that determines the return value of the object's Equals method. Note that this is true only for the current execution of an application, and that a different hash code can be returned if the application is run again. For the best performance, a hash function must generate a random distribution for all input.

shatlin

  • Feb 5th, 2009
 

When the equals() function determined that the two objects are equal, they must generate the same has code.

The reverse is not true. Even if two objects are not equal, they may generated the same hash code.

Mike Lo

  • Apr 25th, 2011
 

Notes to Implementers A hash function is used to quickly generate a number (hash code) that corresponds to the value of an object. Hash functions are usually specific to each Type and, for uniqueness, must use at least one of the instance fields as input. A hash function must have the following properties: If two objects compare as equal, the GetHashCode method for each object must return the same value. However, if two objects do not compare as equal, the GetHashCode methods for the two object do not have to return different values. The GetHashCode method for an object must consistently return the same hash code as long as there is no modification to the object state that determines the return value of the object's Equals method. Note that this is true only for the current execution of an application, and that a different hash code can be returned if the application is run again. For the best performance, a hash function must generate a random distribution for all input.


greg

  • Jul 14th, 2011
 

Of course you can override the equals class and return true whenever you want.

  Was this answer useful?  Yes

sindhu

  • Jul 25th, 2011
 

The answer is true in case if the 2 objects refer to the same table...
The answer is false if they refer to the 2 different tables...

  Was this answer useful?  Yes

Yes, its true.... just run the below example u come to know that:


Code
  1. class Program

  2.     {

  3.         static void Main(string[] args)

  4.         {

  5.             string aa = "hello world";

  6.             string bb = "hello world";

  7.  

  8.             Console.WriteLine(aa.GetHashCode());

  9.             Console.WriteLine();

  10.             Console.WriteLine(bb.GetHashCode());

  11.             Console.WriteLine();

  12.             Console.WriteLine(aa.Equals(bb));

  13.  

  14.         }

  15.     }

PeterVH

  • Aug 18th, 2011
 

Answer is FALSE..

You can override at will and mess it up. Isn't logical to do, but it is technically possible.

Code
  1.     public partial class Form1 : Form

  2.     {

  3.         public Form1()

  4.         {

  5.             InitializeComponent();

  6.         }

  7.  

  8.         private void button1_Click(object sender, EventArgs e)

  9.         {

  10.             var pvh = new Tester { FooBar = 2 };

  11.             var pvh2 = new Tester { FooBar = 8 };

  12.  

  13.             Debug.WriteLine(pvh.Equals(pvh2));

  14.             //True

  15.            

  16.             Debug.WriteLine(pvh.GetHashCode() + " " + pvh2.GetHashCode());            

  17.             //2 8

  18.         }

  19.     }

  20.     public class Tester

  21.     {

  22.         public int FooBar;

  23.  

  24.         public override int GetHashCode()

  25.         {

  26.             return FooBar.GetHashCode();

  27.         }

  28.         public override bool Equals(object obj)

  29.         {

  30.             Tester fooItem = obj as Tester;

  31.             if (fooItem != null)

  32.                 return (fooItem.FooBar % 2) == 0;

  33.             return false;

  34.         }

  35.     }

  Was this answer useful?  Yes

sainirav

  • Aug 26th, 2011
 

Answer is True. Please run the below code

Code
  1.   class Program

  2.     {

  3.         static void Main(string[] args)

  4.         {

  5.             baseClass a = new A();

  6.             baseClass b = new B();

  7.             a = b;

  8.             Console.WriteLine(a.Equals(b));

  9.             Console.WriteLine(a.GetHashCode());

  10.             Console.WriteLine(b.GetHashCode());

  11.             Console.ReadLine();

  12.         }

  13.     }

  14.     public interface baseClass

  15.     {

  16.     }

  17.     public class A : baseClass

  18.     {

  19.     }

  20.     public class B : baseClass

  21.     {

  22.     }

  Was this answer useful?  Yes

Deadbeef66

  • Oct 29th, 2011
 

FALSE

Both Equals() and GetHashCode() can be overridden by the developer for user-defined classes, so there is no guaranteed behavior.

  Was this answer useful?  Yes

Alexandre

  • Oct 13th, 2012
 

Not necessarily. Actually GetHashCode() can always return the same value... :-) Say "1". And everything will work well. Just Hashtable with such a key will not be efficient. Keys in the hashtable are placed in the buckets. If you have several keys which has the same hashcode that means that they will be placed in the same backet. So the Hashtable search engine first tries to find the right bascet by the hashcode value and then it uses Equals method to find the right key in the bucket

  Was this answer useful?  Yes

Manish Kumar

  • Jan 6th, 2015
 

True. When Equals is called, it first goes to the getHashCode() only when the HashCodes are equal it proceed for next checks. In case we override Equals method we should also override GetHashCode method accordingly.

  Was this answer useful?  Yes

Suraj

  • Jan 17th, 2015
 

A : True

Code
  1. Object objA = new Object();

  2.             Object objB = objA;

  3.  

  4.             if (objA.Equals(objB))

  5.             {

  6.                 if (objA.GetHashCode() == objB.GetHashCode())

  7.                 {

  8.                     Console.WriteLine("Answer is A: True");

  9.                 }

  10.                 else

  11.                 {

  12.                     Console.WriteLine("Answer is B: False");

  13.                 }

  14.             }

  Was this answer useful?  Yes

regex

  • Apr 7th, 2015
 

It is clearly false. Proof:

Code
  1. class MyClass

  2.     {

  3.         public int _id;

  4.         public MyClass(int id)

  5.         {

  6.             _id = id;

  7.         }

  8.  

  9.         public bool Equals(MyClass obj)

  10.         {

  11.             return this._id == obj._id;

  12.         }

  13.     }

  14. static void Main(string[] args)

  15.         {

  16.             MyClass obj1 = new MyClass(66);

  17.             MyClass obj2 = new MyClass(66);

  18.  

  19.             Console.WriteLine("Equals():{0}",obj1.Equals(obj2));

  20.             Console.WriteLine("GetHashCode():{0}",obj1.GetHashCode() == obj2.GetHashCode());

  21.  

  22.             Console.ReadKey();

  23.         }

  Was this answer useful?  Yes

ViBi

  • Apr 19th, 2015
 

Two objects that are equal return hash codes that are equal. However, the reverse is not true: equal hash codes do not imply object equality, because different (unequal) objects can have identical hash codes

https://msdn.microsoft.com/en-us/library/system.object.gethashcode%28v=vs.110%29.aspx

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions