0

I have a simple class with 3 public fields and 1 private filed of type array. In the constructor, I would like to initialize the array private field with objects of the class itself

I do the following

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  },
    };
}

I build and run, I get the following error:

System.StackOverflowException: 'Exception of type 'System.StackOverflowException' was thrown.'

1
  • 3
    There is endless recursion in your code (ctor calls ctor, which calls ctor, etc), that's why you get SOE. Maybe you want to hold Students in static array instead? What's the point to make it instance one? Commented May 22, 2019 at 11:34

4 Answers 4

1

It is because you are creating endless implementations of this array as your are creating an array of the class your initializing. that constructor will never be able to finish as each entry in your constructor spawns itself x amount of times. each of those again x amount of times and so continues endlessly

Sign up to request clarification or add additional context in comments.

Comments

1

This is because of an infinite loop (each Student object initializes a _studentArray of other students and so on). You need 2 classes: a Students class that contains the studentArray and the Student class with the StudentID, StudentName and Age properties only.

Comments

0

Your code is recursive and leads to an infinite loop. This happens because

new Student()

calls the parameterless constructor of your Student class, which then tries to instantiate a new Student by calling the constructor again, and so on. I think you are getting where im going?

Comments

0

Since you are having endless recursion like what they are saying, you can create 2 classes. 1 class for your student properties with ctor and 1 class for the student list which may look like this:

Student Class:

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;
    }
}

then 2nd class would be the StudentList where you can use the Add method to add data of your students:

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];
    }
}

You can now initialize the StudentList and use the add method. Hope this helps.

1 Comment

Thanks, this is what I will do

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.