Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GetHashCode() for long primitive

I am writing a EqualityComparer for a LINQ distinct expression and I am not too sure about the GetHashCode overload method. Would the below code be correct? The Id property is a long primitive.

public int GetHashCode(Deal obj)
{
   return ((int)obj.Id) ^ ((int)(obj.Id >> 32)); ;
}
like image 983
yulun Avatar asked Nov 06 '12 04:11

yulun


1 Answers

Probably you should check whether obj is not null. In case of null return 0. Honestly your implementation for long Id is completely the same as .NET Framework GetHashCode for long data type. In other words you can simply call obj.Id.GetHashCode() after not null checking.

like image 177
Kirill Polishchuk Avatar answered Oct 14 '22 17:10

Kirill Polishchuk