Effective C# Item 10 and 11. Understand the Pitfalls of GetHashCode Item 10.
-
Upload
lee-flowers -
Category
Documents
-
view
220 -
download
0
description
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.](https://reader035.fdocuments.us/reader035/viewer/2022062401/5a4d1b917f8b9ab0599c125b/html5/thumbnails/1.jpg)
Effective C#
Item 10 and 11
![Page 2: Effective C# Item 10 and 11. Understand the Pitfalls of GetHashCode Item 10.](https://reader035.fdocuments.us/reader035/viewer/2022062401/5a4d1b917f8b9ab0599c125b/html5/thumbnails/2.jpg)
Understand the Pitfalls of GetHashCode
Item 10
![Page 3: Effective C# Item 10 and 11. Understand the Pitfalls of GetHashCode Item 10.](https://reader035.fdocuments.us/reader035/viewer/2022062401/5a4d1b917f8b9ab0599c125b/html5/thumbnails/3.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062401/5a4d1b917f8b9ab0599c125b/html5/thumbnails/4.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062401/5a4d1b917f8b9ab0599c125b/html5/thumbnails/5.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062401/5a4d1b917f8b9ab0599c125b/html5/thumbnails/6.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062401/5a4d1b917f8b9ab0599c125b/html5/thumbnails/7.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062401/5a4d1b917f8b9ab0599c125b/html5/thumbnails/8.jpg)
Modify previous code
![Page 9: Effective C# Item 10 and 11. Understand the Pitfalls of GetHashCode Item 10.](https://reader035.fdocuments.us/reader035/viewer/2022062401/5a4d1b917f8b9ab0599c125b/html5/thumbnails/9.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062401/5a4d1b917f8b9ab0599c125b/html5/thumbnails/10.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062401/5a4d1b917f8b9ab0599c125b/html5/thumbnails/11.jpg)
Hash is changed by data member
![Page 12: Effective C# Item 10 and 11. Understand the Pitfalls of GetHashCode Item 10.](https://reader035.fdocuments.us/reader035/viewer/2022062401/5a4d1b917f8b9ab0599c125b/html5/thumbnails/12.jpg)
Improved by immutability of the properties
![Page 13: Effective C# Item 10 and 11. Understand the Pitfalls of GetHashCode Item 10.](https://reader035.fdocuments.us/reader035/viewer/2022062401/5a4d1b917f8b9ab0599c125b/html5/thumbnails/13.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062401/5a4d1b917f8b9ab0599c125b/html5/thumbnails/14.jpg)
Prefer foreach Loops
Item 11
![Page 15: Effective C# Item 10 and 11. Understand the Pitfalls of GetHashCode Item 10.](https://reader035.fdocuments.us/reader035/viewer/2022062401/5a4d1b917f8b9ab0599c125b/html5/thumbnails/15.jpg)
Loops
BEST
WORST
![Page 16: Effective C# Item 10 and 11. Understand the Pitfalls of GetHashCode Item 10.](https://reader035.fdocuments.us/reader035/viewer/2022062401/5a4d1b917f8b9ab0599c125b/html5/thumbnails/16.jpg)
Loop3: one bounds check for the price of two
![Page 17: Effective C# Item 10 and 11. Understand the Pitfalls of GetHashCode Item 10.](https://reader035.fdocuments.us/reader035/viewer/2022062401/5a4d1b917f8b9ab0599c125b/html5/thumbnails/17.jpg)
foreach always generates the best code (1/2)
![Page 18: Effective C# Item 10 and 11. Understand the Pitfalls of GetHashCode Item 10.](https://reader035.fdocuments.us/reader035/viewer/2022062401/5a4d1b917f8b9ab0599c125b/html5/thumbnails/18.jpg)
foreach always generates the best code (2/2)
![Page 19: Effective C# Item 10 and 11. Understand the Pitfalls of GetHashCode Item 10.](https://reader035.fdocuments.us/reader035/viewer/2022062401/5a4d1b917f8b9ab0599c125b/html5/thumbnails/19.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062401/5a4d1b917f8b9ab0599c125b/html5/thumbnails/20.jpg)
Example
![Page 21: Effective C# Item 10 and 11. Understand the Pitfalls of GetHashCode Item 10.](https://reader035.fdocuments.us/reader035/viewer/2022062401/5a4d1b917f8b9ab0599c125b/html5/thumbnails/21.jpg)
Resource management
![Page 22: Effective C# Item 10 and 11. Understand the Pitfalls of GetHashCode Item 10.](https://reader035.fdocuments.us/reader035/viewer/2022062401/5a4d1b917f8b9ab0599c125b/html5/thumbnails/22.jpg)
Example
![Page 23: Effective C# Item 10 and 11. Understand the Pitfalls of GetHashCode Item 10.](https://reader035.fdocuments.us/reader035/viewer/2022062401/5a4d1b917f8b9ab0599c125b/html5/thumbnails/23.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062401/5a4d1b917f8b9ab0599c125b/html5/thumbnails/24.jpg)
Weak points
• Can’t modify enumeration member
• Can’t delete member in the conllection