Jump to content
This site uses cookies! Learn More

This site uses cookies!

By continuing to use this site, you agree to allow us to store cookies on your computer. :)

  • 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

    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;
            }

     

    Share this comment


    Link to comment
    Share on other sites


    Join the conversation

    You can post now and register later. If you have an account, sign in now to post with your account.

    Guest
    Add a comment...

    ×   Pasted as rich text.   Paste as plain text instead

      Only 75 emoji are allowed.

    ×   Your link has been automatically embedded.   Display as a link instead

    ×   Your previous content has been restored.   Clear editor

    ×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...