递归类C#

时间:2013-02-13 13:14:26

标签: c# oop class recursion

我可以像这样定义构造函数吗?其他问题:我可以在构造函数中调用类的构造函数吗?

class SubArray
{
    List<int> array;
    string parent;
    string name;
    SubArray child;

    public SubArray(SubArray child, string name)
    {
        this.child = child;
        List<int> array = new List<int>();
        this.name = name;
    }
}

2 个答案:

答案 0 :(得分:11)

没有限制,但像任何递归一样 - 它需要一个停止条件。否则会导致堆栈溢出(PUN打算:))。

答案 1 :(得分:4)

我想你可以做这样的事情而且没有明显的问题:

public SubArray(SubArray child, string name)
{
    this.child = child;
    this.array = new List<int>();
    this.name = name;

    if (child != null && child.child != null)
    {
        this.child.child = new SubArray(child.child,name);
    }
}