简体   繁体   中英

Can GetHashCode() for the same double result in a different integer?

Let's say we have a double value in C#.

Is it possible that the GetHashCode() may return different integer values for this double on different computers/windows/architectures?

    public unsafe override int GetHashCode() {
        double d = m_value; 
        if (d == 0) { 
            // Ensure that 0 and -0 have the same hash code
            return 0; 
        }
        long value = *(long*)(&d);
        return unchecked((int)value) ^ ((int)(value >> 32));
    }

Object.GetHashCode Method says:

The default implementation of the GetHashCode method does not guarantee unique return values for different objects. Furthermore, the .NET Framework does not guarantee the default implementation of the GetHashCode method, and the value it returns will be the same between different versions of the .NET Framework. Consequently, the default implementation of this method must not be used as a unique object identifier for hashing purposes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM