C# 在构造函数中填充数组

C# 在构造函数中填充数组,c#,arrays,constructor,initialization,C#,Arrays,Constructor,Initialization,我有一个简单的类,有3个公共字段和1个数组类型的私有字段。在构造函数中,我想用类本身的对象初始化数组私有字段 我做以下几点 public class Student { public int StudentID { get; set; } public String StudentName { get; set; } public int Age { get; set; } private Student[] _studentArray; public S

我有一个简单的类,有3个公共字段和1个数组类型的私有字段。在构造函数中,我想用类本身的对象初始化数组私有字段

我做以下几点

public class Student
{
    public int StudentID { get; set; }
    public String StudentName { get; set; }
    public int Age { get; set; }
    private Student[] _studentArray;
    public Student()
    {
        _studentArray = new Student[]{
        new Student() { StudentID = 1, StudentName = "John", Age = 18 },
        new Student() { StudentID = 2, StudentName = "Steve",  Age = 21 },
        new Student() { StudentID = 3, StudentName = "Bill",  Age = 25 },
        new Student() { StudentID = 4, StudentName = "Ram" , Age = 20 },
        new Student() { StudentID = 5, StudentName = "Ron" , Age = 31 },
        new Student() { StudentID = 6, StudentName = "Chris",  Age = 17 },
        new Student() { StudentID = 7, StudentName = "Rob",Age = 19  },
    };
}
生成并运行时,出现以下错误:

System.StackOverflowException:'类型的异常 已抛出“System.StackOverflowException”


您的代码是递归的,会导致无限循环。这是因为

new Student()

调用Student类的无参数构造函数,然后该类通过再次调用构造函数来尝试实例化新的Student,依此类推。我想你已经明白我的意思了?

你的代码是递归的,会导致无限循环。这是因为

new Student()

调用Student类的无参数构造函数,然后该类通过再次调用构造函数来尝试实例化新的Student,依此类推。我想你已经找到我要去的地方了?

这是因为你正在创建这个数组的无限实现,而你正在创建初始化类的数组。该构造函数将永远无法完成,因为构造函数中的每个条目都会自己生成x次。这其中的每一个都会重复x次,因此会无休止地继续下去,这是因为在创建初始化类的数组时,您正在创建该数组的无休止的实现。该构造函数将永远无法完成,因为构造函数中的每个条目都会自己生成x次。这其中的每一个都会重复x次,因此会无休止地继续下去,这是因为有一个无限循环(每个Student对象初始化一个其他Student的studentArray,依此类推)。
您需要两个类:一个包含studentArray的Student类和一个仅包含StudentID、StudentName和Age属性的Student类。

这是因为有一个无限循环(每个Student对象初始化其他学生的一个studentArray等等)。
您需要两个类:一个包含studentArray的Student类和一个仅包含StudentID、StudentName和Age属性的Student类。

因为您像他们所说的那样有无休止的递归,所以您可以创建两个类。1个类用于包含ctor的学生属性,1个类用于学生列表,如下所示:

学生班级:

public class Student
{
    public int studentID { get; set; }
    public String studentName { get; set; }
    public int age { get; set; }
    public Student(int StudentID, string StudentName, int Age)
    {
         studentID = StudentID;
         studentName= StudentName;
         age = Age;
    }
}
第二类是学生列表,您可以使用Add方法添加学生的数据:

public class StudentList : Collection<Student>
{
   public Student this[int ctr]
   {
      get{return this.Items[ctr]; }
      set{ this.Items[ctr] = value; }
   }

    new public Student Add(Student newStudent)
    {
        this.Items.Add(newStudent);
        return (Student)this.Items[this.Items.Count-1];
    }
}
公共班级学生名单:集合
{
公立学生本[int ctr]
{
获取{返回此.Items[ctr];}
设置{this.Items[ctr]=value;}
}
新公共学生添加(学生新闻学生)
{
this.Items.Add(newStudent);
返回(学生)this.Items[this.Items.Count-1];
}
}

现在可以初始化StudentList并使用add方法。希望这能有所帮助。

因为你像他们所说的那样有无止境的递归,你可以创建两个类。1个类用于包含ctor的学生属性,1个类用于学生列表,如下所示:

学生班级:

public class Student
{
    public int studentID { get; set; }
    public String studentName { get; set; }
    public int age { get; set; }
    public Student(int StudentID, string StudentName, int Age)
    {
         studentID = StudentID;
         studentName= StudentName;
         age = Age;
    }
}
第二类是学生列表,您可以使用Add方法添加学生的数据:

public class StudentList : Collection<Student>
{
   public Student this[int ctr]
   {
      get{return this.Items[ctr]; }
      set{ this.Items[ctr] = value; }
   }

    new public Student Add(Student newStudent)
    {
        this.Items.Add(newStudent);
        return (Student)this.Items[this.Items.Count-1];
    }
}
公共班级学生名单:集合
{
公立学生本[int ctr]
{
获取{返回此.Items[ctr];}
设置{this.Items[ctr]=value;}
}
新公共学生添加(学生新闻学生)
{
this.Items.Add(newStudent);
返回(学生)this.Items[this.Items.Count-1];
}
}

现在可以初始化StudentList并使用add方法。希望这能有所帮助。

代码中存在无休止的递归(ctor调用ctor,后者调用ctor,等等),这就是为什么会出现SOE。也许你想把
Student
s放在
static
数组中?让它成为一个实例有什么意义呢?代码中有无止境的递归(ctor调用ctor,后者调用ctor,等等),这就是为什么会得到SOE。也许你想把
Student
s放在
static
数组中?举个例子有什么意义?谢谢,这就是我要做的汉克斯,这就是我要做的