3

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.

Community
  • 1
  • 1
Masoud
  • 1,354
  • 6
  • 18
  • 30

2 Answers2

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.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • Marc you are right but see Masoud's problem in previous question http://stackoverflow.com/questions/4634201/using-hash-in-c/4634224#4634224 – Saeed Amiri Jan 08 '11 at 15:01
  • 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. – Masoud Jan 08 '11 at 15:02
  • 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. – atamanroman Jan 08 '11 at 15:03
  • 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 Jan 08 '11 at 15:06
  • @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. – Marc Gravell Jan 08 '11 at 15:13
  • @Masoud: Then you need an IEqualityComparer implementation for arrays. Fortunately I implemented this a few days ago :) http://stackoverflow.com/questions/4598368/compare-objects/4598406#4598406 – Jon Skeet Jan 08 '11 at 15:19
  • @Skeet: can I get same hash code by implementing IEqualityComparer? – Masoud Jan 08 '11 at 15:27
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.

CARLOS LOTH
  • 4,675
  • 3
  • 38
  • 44
  • 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. – Masoud Jan 08 '11 at 15:13
  • 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. – CARLOS LOTH Jan 08 '11 at 15:20
  • 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. – Masoud Jan 08 '11 at 15:31
  • If you override GetHashCode without a matching equals then the implementation is broken. – Marc Gravell Jan 08 '11 at 16:50
  • 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. – CARLOS LOTH Jan 08 '11 at 17:45