yulun
yulun

Reputation: 260

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)); ;
}

Upvotes: 7

Views: 9457

Answers (1)

Kirill Polishchuk
Kirill Polishchuk

Reputation: 56212

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.

Upvotes: 13

Related Questions