在定义我的 class X 时在 class X 中声明类型 X 的变量

declare variable of a type X in class X when defining my class X

我知道标题不清楚,但对不起我的英语我没有找到如何表达我的问题。所以我希望下面我能进一步解释。

事实上,我正在阅读一段代码,其中用户在定义 class(我们称之为 X)时,他声明了 X 的 table(在 class定义)。

来自 C++,这是不可能的,因为类型 X 在那个阶段是不完整的。

还有另一个问题:在 ctor 中,用户分配了 10 个元素,因此认为这可能是一个无限循环,因为对于 X 的每个实例,我们允许 10 个元素,但事实并非如此!

那么 C# 是否允许这种行为?

//here we start the class definition
class X
{
    X[] Items = null;  // and here we declare a table of X elements ???

    public X()
    {
        Items = new X[10];  // and here we allocate 10 elements of X  ???
    }
}

在C#中,类型不完整没有问题。

另一方面,我明白你所说的无限循环是什么意思。但是也不是这样,因为只分配了数组(C++中是指针数组),所以这些指针都是NULL,没有调用默认构造函数。