Search code examples
c#.nethashtreegethashcode

How to write hash code for a tree-like data structure based on the location of the item in the tree?


Each item looks like this:

public interface IEffect
{
    string Name { get; }
    bool Compute ( );

    List<IEffect> SubEffects { get; set; }
    IEffect ElseIfEffect { get; set; }
}

I want to create a tree-like structure using many instances of these items connected to each other forming a tree-like structure. But later I want to hash each item to a Dictionary, so I thought if I could create a hash value based on where they are on the tree, then I could get unique-enough hash values.

Any ideas on how to do this?


Solution

  • based on where they are on the tree

    That information is not part of the node but of the tree. So it would be a very bad idea (defining somethings HashCode on external factors).

    Luckily, as @spintheblack points out, there is absolutely no reason to override GethashCode() here.