C#C
C#3y ago
potzko

❔ help with self referential class typing

I was trying to make a node class I can inherit from and ran into a ton of trouble,
in my original solution the "next" value was still a node rather then the child class, I looked around and people were saying to use a self referential generic class signature so I used this:
public class Node<Self, T>
where Self: Node<Self, T> 
{
    public T value;
    public Self? next;
}

and started writing, I even got some inheritance to work,
public class CmpNode<T>: Node<CmpNode<T>, T>
where T: IComparable
{
    public CmpNode(T value) : base(value) { }
    public CmpNode(T value, CmpNode<T> next) : base(value, next) { }

    public CmpNode<T> getMaxNode(){
        var max = this;
        var point = this;
        while (point!.hasNext()){
            if (max.value.CompareTo(point.value) == -1){
                max = point;
            }
            point = point.next;
        }
        if (max.value.CompareTo(point.value) == -1){
            max = point;
        }
        return max;
    }
}

however I ran into a few problems in some of the functions:
    public static Self? fromArr(T[] arr){
        if (arr.Length == 0){
            return null;
        } 
        var start = new Self(arr[0]);
        var point = start;
        foreach (T item in arr.Skip(1))
        {    
            point.next = new Self(item);
        }
        return start;    
    }

in this function for example, how can I make a new "Self" object? as I do not know what constructors it has. and if I try to build a node I cant set it as next as I expect a Self
object, am I missing a trick to get this to work?
Was this page helpful?