English 中文(简体)
C# getting unique hash from all objects
原标题:

I want to be able to get a uniqe hash from all objects. What more, in case of

Dictionary<string, MyObject> foo

I want the unique keys for:

  • string
  • MyObject
  • Properties in MyObject
  • foo[someKey]
  • foo

etc..

object.GetHashCode() does not guarantee unique return values for different objects. That s what I need.

Any idea? Thank you

最佳回答

Simply put this is not possible. The GetHashCode function returns a signed integer which contains 2^32 possible unique values. On a 64 bit platform you can have many more than 2^32 different objects running around and hence they cannot all have unique hash codes.

The only way to approach this is to create a different hashing function which returns a type with the capacity greater than or equal to the number of values that could be created in the running system.

问题回答

"Unique hash" is generally a contradiction in terms, even in general terms (and it s more obviously impossible if you re trying to use an Int32 as the hash value). From the wikipedia entry:

A hash function is any well-defined procedure or mathematical function that converts a large, possibly variable-sized amount of data into a small datum, usually a single integer that may serve as an index to an array. The values returned by a hash function are called hash values, hash codes, hash sums, or simply hashes.

Note the "small datum" bit - in other words, there will be more possible objects than there are possible hash values, so you can t possibly have uniqueness.

Now, it sounds like you actually want the hash to be a string... which means it won t be of a fixed size (but will have to be under 2GB or whatever the limit is). The simplest way of producing this "unique hash" would be to serialize the object and convert the result into a string, e.g. using Base64 if it s a binary serialization format, or just the text if it s a text-based one such as JSON. However, that s not what anyone else would really recognise as "hashing".

A unique hash code is impossible without constraints on your input space. This is because Object.GetHashCode is an int. If you have more than Int32.MaxValue objects then at least two of them must map to same hash code (by the pigeonhole principle).

Define a custom type with restrained input (i.e., the number of possible different objects up to equality is less than Int32.MaxValue) and then, and only then, is it possible to produce a unique hash code. That s not saying it will be easy, just possible.

Alternatively, don t use the Object.GetHashCode mechanism but instead some other way of representing hashes and you might be able to do what you want. We need clear details on what you want and are using it for to be able to help you here.

As others have stated, hash code will never be unique, that s not the point.

The point is to help your Dictionary<string, MyObject> foo to find the exact instance faster. It will use the hash code to narrow the search to a smaller set of objects, and then check them for equality.

You can use the Guid class to get unique strings, if what you need is a unique key. But that is not a hash code.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签