c# - Create Hash Value on a List? -
I have a I have a unique way "Hush" in this list, it is unique from all the other lists, what is a great way to do this. NET 4? The purpose is to make a type of "monikar" for the lists so that they can be thrown into a line and later found on the basis of their unique value. If you have multiple items in the list with the same hash code, then you can get the wrong results in a dangerous way. For example, consider these inputs: All these different results show that they are all unique collections. great! Now try duplicate: Uh oh ... Since the exact numbers are not really important here and for obvious display we pretend the hash code of three strings or to break it down into binary representation: Now you should see that the order in the list of items in this The implementation is ignored, i.e. There is a problem with the second duplicate even if you believe that it is okay to have the same content in a different sequence (which is not an approach that I would encourage), I do not think That anybody will argue downward behavior will be desirable. Let's try to diversify each list with duplicates. While If you XOR a number with you, you essentially cancel it, eg. XOR from the same number gives you the original result, i.e. So if we go to The hash code is caclulated as: and similarly with list & lt; MyRichObject & gt; There are 50 instances in each instance, each instance has 1 or 2 unique properties, but in a way they are all unique because there is only one place in the list, etc.
TL; DR
public static GetSequenceHashCode & lt; T & gt; (This is the IList & lt; T & gt; Sequence) {const int seeds = 487; Const int modifier = 31; Uncontrolled {Return Sequence. Aggregate (Seed, (Current, Item) => (Current * Modifier) + Item. GetHashCode ()); }}
Why bother with another answer?
var a = new [] {"foo"}; Var b = new [] {"foo", "bar"}; Var c = new [] {"foo", "bar", "spam"}; Var d = new [] {"Schoadevil", "Hernovil", "Boulenville"};
var e = new [] {"foo", "bar", "spam"};
GetSequenceHashCode should produce the same result for both
c and
e - and this So far everything is fine. Let's try out the sequence of items:
var f = new [] {"spam", "bar", "foo"};
GetSequenceHashCode indicates that both
f can be found in both
c and
E is the e-code which is not it. Why is this happening? Using the
c as an example, first break it into the actual hash code values:
int hashC = "foo" .GetHashCode () ^ "bar" GetHashCode () ^ "Spam" .GetHashCode ();
foo = 8 ,
bar = 16 and
spam = 32 . Therefore:
is has c = 8 ^ 16 ^ 32;
8 ^ 16 ^ 32 == 56; // 8 = 00001000 // ^ // 16 = 00010000 // ^ // 32 = 00100000 // // 56 00111000
8 ^ 16 ^ 32 = 16 ^ 8 ^ 32 = 32 ^ 16 ^ 8 etc.
var a = new [] {"foo", "bar", "spam"}; Var b = new [] {"foo", "bar", "spam", "foo"}; Var c = new [] {"foo", "bar", "spam", "foo", "foo"}; Var d = new [] {"foo", "bar", "spam", "foo", "foo", "spam", "foo", "spam", "foo"};
a and
b generate different queues for hash,
GetSequenceHashCode suggests that
A ,
c and
d are all the same. Why?
8 ^ 8 == 0; // 8 = 00001000 // ^ // 8 = 00001000 // = // 0 = 00000000
8 ^ 8 ^ 8 == 8; // 8 = 00001000 // ^ // 8 = 00001000 // ^ // 8 = 00001000 // = // 8 = 00001000
a And
c again, simplify hash codes replace:
var a = new [] {8, 16, 32}; Var c = new [] (8, 16, 32, 8, 8};
int hashA = 8 ^ 16 ^ 32; // = 56 into hash C = 8 ^ 16 ^ 32 ^ 8 ^ 8; // = 56 / one ???? A ???? // both of these two out of each other
D where
foo and
each pair of spam > cancels itself .
Comments
Post a Comment