Consider the following code:
struct Vec2 : IEquatable
{
double X,Y;
public bool Equals(Vec2 other)
{
return X.Equals(other.X
Jon skeet has this covered:
What is the best algorithm for an overridden System.Object.GetHashCode?
public override int GetHashCode()
{
unchecked // Overflow is fine, just wrap
{
int hash = 17;
// Suitable nullity checks etc, of course :)
hash = hash * 23 + X.GetHashCode();
hash = hash * 23 + Y.GetHashCode();
return hash;
}
}
Also, change your Equals(object)
implementation to:
return Equals(obj as FVector2);
Note however that this could perceive a derived type to be equal. If you don't want that, you'd have to compare the runtime type other.GetType()
with typeof(FVector2)
(and don't forget nullity checks) Thanks for pointing out it's a struct, LukH
Resharper has nice code generation for equality and hash code, so if you have resharper you can let it do its thing