简体   繁体   中英

GetHashCode of System.Type returns different values

Why does GetHashCode returns different values for the same type. If i execute this code:

Console.WriteLine(typeof(Guid).GetHashCode());

In different applications, i get different hash codes.

If i execute the following statement multiple times in different applications:

Console.WriteLine("ABC".GetHashCode());

I get always the same hash code. But why is the hash code changing for System.Type , but not for System.String ?

Thank you.

Neither System.String nor System.Type guarantees persistable hashcodes as a part of its contract. The fact that it so happens to work with System.String in your particular case is an implementation detail that cannot be relied upon. If you need to persist or export a hash of a string, use a different string hashing method. Persisting or exporting information about a type should probably use information like FullName , AssemblyQualifiedName , and others, depending on the exact requirement.

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