GetHashCode () for a long primitive

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

public int GetHashCode(Deal obj) { return ((int)obj.Id) ^ ((int)(obj.Id >> 32)); ; } 
+4
source share
1 answer

You should probably check to see if obj null. In case of zero return 0 . Honestly, your implementation for long Id is exactly the same as the .NET Framework GetHashCode for long . In other words, you can just call obj.Id.GetHashCode() after checking with non-zeros.

+9
source

Source: https://habr.com/ru/post/1444250/


All Articles