Effective C# Item 10 and 11. Understand the Pitfalls of GetHashCode Item 10.

24
Effective C# Item 10 and 11

description

Where to use? Define the hash value for keys in a hash-based collection – Hashtable – Dictionary

Transcript of Effective C# Item 10 and 11. Understand the Pitfalls of GetHashCode Item 10.

Page 1: Effective C# Item 10 and 11. Understand the Pitfalls of GetHashCode Item 10.

Effective C#

Item 10 and 11

Page 2: Effective C# Item 10 and 11. Understand the Pitfalls of GetHashCode Item 10.

Understand the Pitfalls of GetHashCode

Item 10

Page 3: Effective C# Item 10 and 11. Understand the Pitfalls of GetHashCode Item 10.

Where to use?

• Define the hash value for keys in a hash-based collection– Hashtable– Dictionary

Page 4: Effective C# Item 10 and 11. Understand the Pitfalls of GetHashCode Item 10.

Three rules

• If two objects are equal (as defined by operator==), they must generate the same hash value

• For any object A, A.GetHashCode() must be an instance invariant

• The hash function should generate a random distribution among all integers for all inputs

Page 5: Effective C# Item 10 and 11. Understand the Pitfalls of GetHashCode Item 10.

Issues with Object.GetHashCode()

• Rule 1: Pass• Rule 2: Pass• Rule 3: Failed

Page 6: Effective C# Item 10 and 11. Understand the Pitfalls of GetHashCode Item 10.

ValueType.operator==() compares the first field in the struct

The following code snippet always returns true

Page 7: Effective C# Item 10 and 11. Understand the Pitfalls of GetHashCode Item 10.

Check 3 rules again

• Rule 1: Pass *• Rule 2: Pass *• Rule 3: Pass *

Page 8: Effective C# Item 10 and 11. Understand the Pitfalls of GetHashCode Item 10.

Modify previous code

Page 9: Effective C# Item 10 and 11. Understand the Pitfalls of GetHashCode Item 10.

Review Rule 1

• if two objects are equal, as defined by operator==(), they must return the same hash value

• => The same data elements should participate in both computations

Page 10: Effective C# Item 10 and 11. Understand the Pitfalls of GetHashCode Item 10.

Review Rule 2

• the return value of GetHashCode() must be an instance invariant

Page 11: Effective C# Item 10 and 11. Understand the Pitfalls of GetHashCode Item 10.

Hash is changed by data member

Page 12: Effective C# Item 10 and 11. Understand the Pitfalls of GetHashCode Item 10.

Improved by immutability of the properties

Page 13: Effective C# Item 10 and 11. Understand the Pitfalls of GetHashCode Item 10.

Review Rule 3

• GetHashCode() should generate a random distribution among all integers for all inputs– A common and successful algorithm is to XOR all

the return values from GetHashCode() on all fields in a type

– exclude mutable fields from the calculations.

Page 14: Effective C# Item 10 and 11. Understand the Pitfalls of GetHashCode Item 10.

Prefer foreach Loops

Item 11

Page 15: Effective C# Item 10 and 11. Understand the Pitfalls of GetHashCode Item 10.

Loops

BEST

WORST

Page 16: Effective C# Item 10 and 11. Understand the Pitfalls of GetHashCode Item 10.

Loop3: one bounds check for the price of two

Page 17: Effective C# Item 10 and 11. Understand the Pitfalls of GetHashCode Item 10.

foreach always generates the best code (1/2)

Page 18: Effective C# Item 10 and 11. Understand the Pitfalls of GetHashCode Item 10.

foreach always generates the best code (2/2)

Page 19: Effective C# Item 10 and 11. Understand the Pitfalls of GetHashCode Item 10.

Automatically casts each operand

Any hand-coded for loops are broken

Page 20: Effective C# Item 10 and 11. Understand the Pitfalls of GetHashCode Item 10.

Example

Page 21: Effective C# Item 10 and 11. Understand the Pitfalls of GetHashCode Item 10.

Resource management

Page 22: Effective C# Item 10 and 11. Understand the Pitfalls of GetHashCode Item 10.

Example

Page 23: Effective C# Item 10 and 11. Understand the Pitfalls of GetHashCode Item 10.

Benefits

• generates the right code for upper and lower bounds in arrays

• iterates multidimensional arrays• coerces the operands into the proper type• generates the most efficient looping

constructs

Page 24: Effective C# Item 10 and 11. Understand the Pitfalls of GetHashCode Item 10.

Weak points

• Can’t modify enumeration member

• Can’t delete member in the conllection