Question

I am trying to create a recursive Tuple in C#. One side is a reference a class, and the other side goes into recursion to a likewise datastructure. The right side can be null, and this is when the recursion ends.

At the moment I am using an alias, but this does not seem to work.

using Relation = System.Tuple<Node, Relation>;

Directly typing this is not possible, as this goes infinite.

System.Tuple<Node, System.Tuple<Node, System.Tuple<Node, ...>>>

1. Is this possible in C#?

2. Can I do this with aliases?

Extra info

I am doing this because I need to keep track the parent of each node. A node can have multiple children. I cannot edit the node class.

I only need to go up the hierarchy. i.e. I don't need to know a nodes children. I use this for an algorithm where I calculate alot of nodes, and when I find one, I want to find the path to it from bottom to root.

Was it helpful?

Solution

You can create a Class for this.

class Relation
{
   Node Node;  // Node is another class
   Tuple<Node, Relation> Relation;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top