Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different Hash code for same arrays in C#

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.

like image 746
Masoud Avatar asked Oct 25 '25 14:10

Masoud


1 Answers

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.

like image 68
Marc Gravell Avatar answered Oct 27 '25 02:10

Marc Gravell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!