4

This is pseudo code

sbyte[] array1 = new sbyte[100]; 
array1.setValues(); 
sbyte[] array2 = (sbyte[])array1.Clone();

But array1.getHashCode() is not equal with array2.getHashCode();

What should I do to get same hash code for array1 and array2?

P.S: Dictionary isn't useful. Because I wanted to add these arrays to a hashtable. And after each adding I need to check for a possible same content array added before.

P.S2: It seems I should clarify my problem. At first I posted the complete question at Using Hash in C# and when it was solved this question was raised.

3
  • Thanks for your editing. How can I highlight the code parts? Commented Jan 8, 2011 at 15:08
  • Select code and use ctrl + K Commented Jan 8, 2011 at 15:16
  • You keep saying dictionay ist useful because you want to use a hashtable. A dictionary is a hash-table... Commented Jan 8, 2011 at 16:52

2 Answers 2

13

That isn't the same array - it is a different array with the same contents.

An array GetHashCode() doesnt hash the contents. That hash is simple a hash of the reference.

You could write a custom IEqualityComparer<sbyte[]> if needed for a dictionary etc.

Sign up to request clarification or add additional context in comments.

7 Comments

Marc you are right but see Masoud's problem in previous question stackoverflow.com/questions/4634201/using-hash-in-c/…
Dictionary is too slow. I want to add those arrays to a hash table. And check for any possible added same array. The speed is very important for me. Hash will find any object in one or two memory refrence. So dictionary isn't useful.
Note: arrays are not equal after cloning, too. So the hashing and equality contracts are still intact and what your seeing is the desired behaviour.
Thanks saeed for your help. I tried to specify my problem in another post. My first problem was solved in other post. But it seems another problem raised. And I wrote another post.
@masoud a: what do you mean dictionary is too slow? By what measure? Also - you state you want different hash-codes but ask why they aren't the same: please clarify which you want?? But a dictionary with custom comparer should be ideal here.
|
1

There is no way to override the GetHashCode method from the array class. But you can wrap you array inside another class and them use some of its items to specify its hash key.

public class WrappedArray
{
  sbyte[] _inner = new sbyte[10]

  //...

  override public int GetHashCode()
  {
    return _inner[0] ^ _inner[1] ^ _inner[2];
  }
}

It is just an idea. The hash codes don't need to be unique for each object, because hash structures are designed to deal with collisions.

5 Comments

It is a good idea. But there is a problem. The array isn't so predictable. Lots of arrays have same value in lots of their blocks. And just few differences in unknown blocks.
The idea of hashing is to improving the performance by trying to achive the O(1) complexity. However, it accepts the fact that sometimes it is not possible (that is why it was designed to deal with collisions). Two different objects may have the same hashcode, they will be placed on the same bucket, and all the objects on that bucket will be compared for equality by calling the Equals method. Equals method is the bottom line to define if two objects are equal or not, hashcode is only a "trick" for improving access time on collections.
It is true Carlos. I have for example 20000 arrays. And I know I have 10000 pair of same content arrays. But i don't know were the differences may occur. So I need to use a good hash code. Or to implement it.
If you override GetHashCode without a matching equals then the implementation is broken.
Two objects considered equal must have the same hash code, but the opposite is not necessarily true. Two different objects can have the same hash code. On Framework Design Guidelines, page 226, Christopher Brumme says: "If obj1.Equals(obj2) returns true, both of the objects should have the same hash code. If the objects aren't equal, they might or might not have the same hash code. Strictly speaking, all objects could have a hash code of 1. This would really be terrible from a performance point of view when looking up such items in a hash table, of course". I agree 100% with him.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.