C#为类本身中的同一个类创建对象

时间:2022-09-25 10:19:53

I am just trying out some things in c# and i came across this where i have created an object for the same class in the class itself

我只是在c#中尝试了一些东西,我遇到了这个,我在类本​​身为同一个类创建了一个对象

class Class1
    {
        public int test1 { get; set; }
        Class1 x = new Class1();
    }

and then i have tried to create object from an other class which is throwing an error

然后我试图从另一个类中创建一个抛出错误的对象

An unhandled exception of type 'System.*Exception' occurred in Test.exe

Test.exe中发生了未处理的“System.*Exception”类型异常

class Program
    {
        static void Main(string[] args)
        {
            Class1 x = new Class1();
        }
    }

i have googled and i havent found any related links to c# but i found it in c++ where they say a Class1 is an incomplete type, as it has not been defined yet, rather it's being defined. I would like to know whether its the same case for c# also

我已经google了,我没有找到任何相关的链接到c#,但我发现它在c ++,他们说Class1是一个不完整的类型,因为它尚未定义,而是它被定义。我想知道c#是否也是一样的情况

4 个解决方案

#1


Class1 creates a Class1, which creates another Class1, which creates another Class1, which ...

Class1创建一个Class1,它创建另一个Class1,它创建另一个Class1,...

So each constructor call gets added to the execution stack until it overflows.

因此,每个构造函数调用都会添加到执行堆栈中,直到它溢出为止。

Since you don't specify what you want to do with the instance there's no way to know what the right answer is.

由于您没有指定要对实例执行的操作,因此无法知道正确答案是什么。

For good geek humor, Google recursion

为了好极客幽默,谷歌递归

#2


It's pretty straightforward, your problem is every time you create a Class1, that object creates its own Class1. Hence, you're going to recurse until you blow up.

它非常简单,每次创建Class1时都会遇到问题,该对象会创建自己的Class1。因此,你会在你爆炸之前递归。

You can debug it and watch what happens. Set a breakpoint within Class1 somewhere and watch it get hit over and over.

你可以调试它并观察会发生什么。在某个地方的Class1中设置断点并观察它一次又一次地被击中。

#3


A class can contain an object of itself, but you can't instantiate that object in constructor or at the time of declaration. This will cause infinite recursion and * exception.

类可以包含自身的对象,但是您无法在构造函数中或在声明时实例化该对象。这将导致无限递归和*异常。

You can have a separate method to instantiate like:

您可以使用单独的方法来实例化:

class Class1
{
    public int test1 { get; set; }
    private Class1 x;

    public void CreateClass1()
    {
        x = new Class1();
    }
}

and then call it like:

然后称之为:

Class1 obj = new Class1();
obj.CreateClass1();

#4


Just don't create instance of Class1 during creation of Class1 instance. Put a placeholder instead:

只是在创建Class1实例期间不要创建Class1的实例。改为占位符:

class Class1
{
    public int test1 { get; set; }
    public Class1 AnotherClass1;
}

And provide reference to another (already created instance) either after creation or during creation as constructor argument:

并在创建之后或在创建期间提供对另一个(已创建的实例)的引用作为构造函数参数:

static void Main(string[] args)
{
    Class1 x = new Class1();
    x.AnotherClass1 = new Class1();
}

#1


Class1 creates a Class1, which creates another Class1, which creates another Class1, which ...

Class1创建一个Class1,它创建另一个Class1,它创建另一个Class1,...

So each constructor call gets added to the execution stack until it overflows.

因此,每个构造函数调用都会添加到执行堆栈中,直到它溢出为止。

Since you don't specify what you want to do with the instance there's no way to know what the right answer is.

由于您没有指定要对实例执行的操作,因此无法知道正确答案是什么。

For good geek humor, Google recursion

为了好极客幽默,谷歌递归

#2


It's pretty straightforward, your problem is every time you create a Class1, that object creates its own Class1. Hence, you're going to recurse until you blow up.

它非常简单,每次创建Class1时都会遇到问题,该对象会创建自己的Class1。因此,你会在你爆炸之前递归。

You can debug it and watch what happens. Set a breakpoint within Class1 somewhere and watch it get hit over and over.

你可以调试它并观察会发生什么。在某个地方的Class1中设置断点并观察它一次又一次地被击中。

#3


A class can contain an object of itself, but you can't instantiate that object in constructor or at the time of declaration. This will cause infinite recursion and * exception.

类可以包含自身的对象,但是您无法在构造函数中或在声明时实例化该对象。这将导致无限递归和*异常。

You can have a separate method to instantiate like:

您可以使用单独的方法来实例化:

class Class1
{
    public int test1 { get; set; }
    private Class1 x;

    public void CreateClass1()
    {
        x = new Class1();
    }
}

and then call it like:

然后称之为:

Class1 obj = new Class1();
obj.CreateClass1();

#4


Just don't create instance of Class1 during creation of Class1 instance. Put a placeholder instead:

只是在创建Class1实例期间不要创建Class1的实例。改为占位符:

class Class1
{
    public int test1 { get; set; }
    public Class1 AnotherClass1;
}

And provide reference to another (already created instance) either after creation or during creation as constructor argument:

并在创建之后或在创建期间提供对另一个(已创建的实例)的引用作为构造函数参数:

static void Main(string[] args)
{
    Class1 x = new Class1();
    x.AnotherClass1 = new Class1();
}