Your code should work without any problems in .NET, and I would dare to say that it will remain the same even if the version of .NET changes. The fact is that the dictionary uses two things to access the value using a specific key: Equals () and GetHashCode () as follows
- get all the keys corresponding to the required key value with the value GetHashCode () (the value of GetHashCode should be equal for equal objects and should be different for different objects)
- filter the list created in the previous step using Equals ().
The standard Equals () values ββfor value types use reflection to access fields and compare them (as mentioned earlier). Bearing this in mind, the default value of GetHashCode () should correspond to how the default implementation of Equals () works, if a == b, a.GetHashCode () == b.GetHashCode () - in any case, there would be no meaning in providing a default if it does not even meet the minimum minimum. What MSDN says GetHashCode () does not provide unique values, and that is understandable. Keep in mind that such a simple implementation:
public int GetHashCode() { return 0; }
still valid (dictionaries, hashtables, etc. will work), even if the values ββare not unique. Performance, of course, is another matter, the use of such an implementation will lead to a full scan of each collection and testing of each element for equality.
source share