0

I am really confused how to use my class for a key in a dictionary.

I need to store key value pairs of my node class so i know what path i have traversed on my double connected graph.

So i need to create a unique hash for my class to use the node as a key. But i have no idea what i am meant to do to implement it.

My node class is a simple one:

  public class Node
  {
        public Vector2 Position;
        public List<Node> Connections;
  }

And in my graph traversal script I need to use a dictionary like this:

    //store paths "From"=> key, "To"=> value
    Dictionary<Node, Node> _visitedPaths;

On the Microsoft website it says:

Two objects that are equal return hash codes that are equal. However, the reverse is not true: equal hash codes do not imply object equality, because different (unequal) objects can have identical hash codes.

So if my understanding on this is correct, two different objects can create identical hash codes, so I need to implement my own hash code but I am not sure how I can guarantee to generate a unique hash code for each node object.

WDUK
  • 1,412
  • 1
  • 12
  • 29
  • See especially https://stackoverflow.com/a/56539595/3538012 – Peter Duniho Jul 04 '21 at 02:28
  • If you have more than 4,294,967,295 nodes then you can't provide a unique hash for each node. This is why the quoted statement is true. You need to check equality in addition to matching hash codes. – ProgrammingLlama Jul 04 '21 at 02:39

0 Answers0