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.'
Students instaticarray instead? What's the point to make it instance one?