What's the meaning of CS0659?
I got this warning message but i cannot find what's the reason I should override GetHashCode. Also, how should I implement it?
15 Replies
ur trying to make a class that implements the IEqualityComparer interface right?
uhm wait
this is how it now looks like
i copy pasted ur code, it does not give me ur error
u sure the error is not somewhere else?
anyway you can just override GetHashCode like you override any method
i'm on .net 6
so it does implement the IEquatable interface
does this matter?
uh yeah i just added it to see if it solves the problem
CS0659 is a warning https://learn.microsoft.com/en-us/dotnet/csharp/misc/cs0659
yeah it says "An override of Equals implies that you also want to override GetHashCode." but idk why
It's to ensure correct, consistent behavior when using both equals and getting a hash.
eg. for
Dictionary
and HashSet
.
You can override GetHashCode
and use https://learn.microsoft.com/en-us/dotnet/api/system.hashcode.combine?view=net-7.0 which is good enough.
You probably shouldn't be creating your own vector type though.okay got it thanks
uh wait
so the 'correct, consistent behavior' means 'two hash codes are equal if and only if Equals() == true'?
It means that the equals and hash code calculation should both rely on the exact same subset of properties.
Imagine if your Equals uses X and Y, but your hash code uses X, Y, and Z.
i got it
what would be a great alternative rather than manually implementing a vector type?
Vector3?
Depends on what you're trying to accomplish, perf requirements, what libraries you are or can use, etc.
reasonable
thanks all, anyways!