Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

object.GetHashCode not working #390

Closed
MatthiasJentsch opened this issue Sep 1, 2018 · 8 comments · Fixed by nanoframework/nf-interpreter#852
Closed

object.GetHashCode not working #390

MatthiasJentsch opened this issue Sep 1, 2018 · 8 comments · Fixed by nanoframework/nf-interpreter#852

Comments

@MatthiasJentsch
Copy link
Contributor

MatthiasJentsch commented Sep 1, 2018

Details about Problem

nanoFramework area: nanoCLR

Worked before? I think no.

Detailed repro steps so we can see the same problem

Write this C# program:

object a = new object();
object b = new object();
object c = a;
int ha = a.GetHashCode();
int hb = b.GetHashCode();
int hc = c.GetHashCode();

And all three (ha, hb and hc) are zero. GetHashCode returns alsways zero.
Tested with Prewiew 791 on an ESP32.

@MatthiasJentsch
Copy link
Contributor Author

That's not working because object has no fields. It's also not working for all instances that has no fields.

@josesimoes
Copy link
Member

After some investigation I'm tempted to suggest that we should leave it as it is.

See the official docs here
The hash code of object shoul NOT be used as unique object identifier for hashing purposes.

Also about the native implementation here.

@josesimoes
Copy link
Member

Any other thoughts on this? 😉

@NemesisXB
Copy link
Contributor

Even though it should not be used as a unique object identifier, won't it be better to at least have it work like the standard implementation?
Otherwise, should it not at least throw not implemented?

@josesimoes
Copy link
Member

OK. Digging deeper I could find an old native implementation for this call here

Guess we can try to accomplish something similar for objects, instead of relying on calculating the hash from fields. Or, perhaps, if there are no fields then return the CRC32 of the heap block address?

@MatthiasJentsch
Copy link
Contributor Author

I think calculating the hash of the fields is in general a good idea. Implementing a complicated algorithm would waste memory and time. I thinks we should improve what we have and return the CRC32 of the heap block address if the instance has no fields.

@josesimoes
Copy link
Member

Suppose we have an agreement on this. Let's go for it

@josesimoes
Copy link
Member

Suppose we have an agreement on this. Let's go for it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants