Autor Beitrag
georgeboy Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 198



BeitragVerfasst: So 20.12.20 08:08 
Wieso ist denn die Hashfunktion ein schneller Vorvergleich ? Vergleichen tut doch nur Equals ? Die Hashfunktion ( GetHashCode ... ) berechnet doch nur aus dem Schlüssel einen Index für die Hashtabelle ? Da verstehe ich wahrscheinlich grundlegende Dinge nicht.
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4764
Erhaltene Danke: 1052

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: So 20.12.20 10:21 
Bevor die evtl. aufwendige Equals-Methode aufgerufen wird, wird der Hashwert verglichen:
ausblenden C#-Quelltext
1:
2:
3:
4:
if (GetHashCode(a) != GetHashCode(b))
    return false;

return Equals(a, b);

Schau dir dazu z.B. die Implementierung innerhalb der Dictionary<K, V>-Klasse an: FindEntry (selbiges bei Insert und Remove). Nur bei gleichem Hashwert wird Equals aufgerufen.
georgeboy Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 198



BeitragVerfasst: So 20.12.20 11:03 
Genial ! Danke !!!