Jump to content
  • GetHashCode() & Equals() position implementation


    reapler
    • Version: All Product: WRobot General Type: Suggestion Status: Added

    The following classes can have these overrides implemented:

    Vector3
    TaxiNode

    A correct implementation would look like this:

            public override int GetHashCode()
            {
                return (Position.X.GetHashCode() * 397 ^ Position.Y.GetHashCode()) * 397 ^ Position.Z.GetHashCode();
            }
    
            public override bool Equals(object obj)
            {
                Node rhs = obj as Node;
                if (rhs == null)
                    return false;
                return Position.X == rhs.Position.X
                    && Position.Y == rhs.Position.Y
                    && Position.Z == rhs.Position.Z;
            }

    This will enable the usage of the class in Dictionary<> and HashSet<>.

    These collections provide higher performance and uniqueness of each inserted element.

    List<> collection's behavior will stay the same.



    User Feedback

    Recommended Comments

    Droidz

    Posted

    Hello, thank you,

    I added it, wait next update:

            public override int GetHashCode()
            {
                try
                {
                    return (X.GetHashCode() * 397 ^ Y.GetHashCode()) * 397 ^ Z.GetHashCode();
                }
                catch
                { }
                return 0;
            }
    
            public override bool Equals(object obj)
            {
                try
                {
                    var v = obj as Vector3;
                    if (v == null)
                        return false;
                    return X == v.X
                           && Y == v.Y
                           && Z == v.Z;
                }
                catch
                { }
                return false;
            }

     



    Create an account or sign in to comment

    You need to be a member in order to leave a comment

    Create an account

    Sign up for a new account in our community. It's easy!

    Register a new account

    Sign in

    Already have an account? Sign in here.

    Sign In Now

×
×
  • Create New...