0

Recently I have asked a question about GetHashCode GetHashCode for the object with several data members

After checking http://msdn.microsoft.com/en-us/library/ms132155(v=vs.110).aspx, looks that if the input for GetHashCode is null the exception should be thrown.

Is it correct ?looks that the correct implementation should be :

public int GetHashCode(MyClass myobj)
{
     if(myObj == null)
     {
        //Throw exception ? 
        //return base.GetHashCode(); <-- not correct?
     }
     return (myObj.x != null ? myObj.x.GetGashCode() : 0) ^ (myObj.y != null ? myObj.y.GetGashCode()
}
Community
  • 1
  • 1
YAKOVM
  • 9,805
  • 31
  • 116
  • 217
  • The page you linked does *not* demand that an NRE is thrown, it is merely a common outcome. And in fact the .NET framework version of `EqualityComparer.Default` does not. It does need to permit collections that may contain null elements, it isn't illegal. It is up to you to decide for yourself if that's okay in your code. If your team lead insists that you throw then you'd be wise to follow his lead. – Hans Passant Mar 16 '14 at 13:09

1 Answers1

0

yes thats fine. No exception should be thrown

Vaibhav
  • 2,073
  • 4
  • 23
  • 26
  • Why?My Lead told me that it is not ok and I should check that ,since following msdn : IEqualityComparer.GetHashCode throws an exception for null argument – YAKOVM Mar 16 '14 at 12:07