C# - Generic HashCode implementation for classes -


I see how to make the best hashcode for a class and I see some algorithms. I have seen this one: It seems that the .NET classes havehashcode methods are the same (see by showing the code).

So the question is, why not to create a static class above, the hashcode automatically passes through the field, we consider it as a "key".

  // Older version, Public static class HashCodeBuilder {public static int hash (params object [] keys) Edit {if (Object.Reference Aquals (keys, blank)) {Return 0 ; } Int number = 42; Check (for {int i = 0, length = keys.labee; i   

and use it like this:

  // Edit the old version, public sealed section View: Equitable & lt; A & gt; {Public A () {} Public String Key1 {get; Set; } Public string key2 {get; Set; } Public string value {get; Set; } Public override bull par (object obje) {Return to it. Eclairs (ABZA); } Public bool par (another) {if (object.ReferenceEquals (other, empty))? Incorrect: Key1 == Other Kay 1 & amp; Amp; Key2 == Other K2; } Public override intoshashcode () {hashcodebilder. Return (Key 1, Key 2); }}   

Will be very simple that is always its method, no? I'm missing something?


Edit

According to all comments, I have received the following code:

  Public static class hashcodebilder {public static int hash ( Paramaz object [] Args) {if (args == null) {return 0; } Int number = 42; Uncontrolled {foreach (different items in Args) {if (context ac, null)) {} and if (item gate type). IARRA) {foreach (var subItem in (IEnumerable items)} {num = num * 37+ hash (all items); }} And {num = num * 37+ item.GetHashCode (); }}} Return number; }} Public Sealed Category A: IEquatable & lt; A & gt; {Public A () {} Public String Key1 {get; Set; } Public string key2 {get; Set; } Public string value {get; Set; } Public override bull par (object obje) {Return to it. Eclairs (ABZA); } Public bool par (another) {if (referenced (other, empty)) {return false; } And if (context aquas (this, others)) {back true; } Return key1 == Other K1 & amp; Amp; Key2 == Other K2; } Public override intoshashcode () {hashcodebilder. Return (Key 1, Key 2); }}    

Your equal method has been broken - assuming that the same hash With two object codes are essentially equal. This is not just the case.

Your hash code method was shown right at a quick glance, but actually did something with some work - see below it means that the values ​​of any value type are boxing And Any time you call it, you create an array, but besides this it is fine (as Slakes said, there are some problems around collection collection). You may want to consider writing a few general surcharges which will avoid those performance penalties for general matters (probably for 1, 2, 3 or 4 arguments).

You can do the same You can also do

sort of thing for equality, but it will be a bit difficult and Messier .

EDIT: For the hash code, you are only adding value I Doubts You were trying to do something like this:

 < Code> int hash = 17; Hash = hash * 31 + first value. GetHashCode (); Hash = hash * 31 + second value. GetHashCode (); Hash = hash * 31 + third value. GetHashCode (); Return hash;   

But it is that multiplies hash 31, it does not add 31. At present your hash code will always always come back for the same value, whether they are in the same order or not, which is not ideal.

EDIT: It seems that there is some confusion about the use of the hash code. I suggest that those who definitely do not forget to read the document for Eric Lipert.

Comments

Popular posts from this blog

qt - switch/case statement in C++ with a QString type -

python - sqlite3.OperationalError: near "REFERENCES": syntax error - foreign key creating -

Python's equivalent for Ruby's define_method? -