0

I have a simple class ,and i want to initialize it in the constructor as you can see here:

 public class CameraSetting
    {

        public  string CameraIP { set; get; }
        public  string CameraType { set; get; }
        public int CameraGroup { set; get; }
        public string GateId { set; get; }

        public List<CameraSetting> MyList { set; get; }
        public CameraSetting()
        {
            MyList = new List<CameraSetting>()
            {
                new CameraSetting() { CameraIP = "192", CameraGroup = 0, CameraType = "ورود",GateId = "1"},
                new CameraSetting() { CameraIP = "193", CameraGroup = 0, CameraType = "خروج",GateId = "1"},
                new CameraSetting() { CameraIP = "194", CameraGroup = 1, CameraType = "ورود",GateId = "2"},
                new CameraSetting() { CameraIP = "195", CameraGroup = 1, CameraType = "خروج",GateId = "2"}

            };
        }
    }

In my code i call CameraSetting obj=new CameraSetting();.but it returns this error :

{"Exception of type 'System.StackOverflowException' was thrown."}

enter image description here

1
  • 2
    You're recursively calling the constructor inside of the constructor. Commented Feb 9, 2017 at 4:18

2 Answers 2

1

It's because each constructor creates four instances of the same type and the operation never ends. This is the order in which it happens...

  1. You create CameraSetting 1
  2. Then, CameraSettings instantiate 4 other CameraSettings
  3. Then, each of these CameraSettings create another 4 CameraSettings EACH! That is, 4 * 4 = 16
  4. Repeat indefinitely until the runtime kills the process with a StackOverflowException
Sign up to request clarification or add additional context in comments.

1 Comment

Minor correction... each call to the constructor calls the same constructor recursively until you run out of stack space. The 4 * 4 * 4 * ... series is not realized as each step in the process will recurse once, never returning to do the second.
1

Each time when you create an object of CameraSetting to add items to List, the constructor is invoked which forms a infinite loop here and that cause the exception.

I think it's better to take the List from the class and define it somewhere else as you needed. It is fine to use Like this in your main or some other places as required:

static void Main(string[] args)
{
    List<CameraSetting> _MyList = new List<CameraSetting>()
    {
        new CameraSetting() { CameraIP = "192", CameraGroup = 0, CameraType = "ورود",GateId = "1"},
        new CameraSetting() { CameraIP = "193", CameraGroup = 0, CameraType = "خروج",GateId = "1"},
        new CameraSetting() { CameraIP = "194", CameraGroup = 1, CameraType = "ورود",GateId = "2"},
        new CameraSetting() { CameraIP = "195", CameraGroup = 1, CameraType = "خروج",GateId = "2"}
    };
  // Rest of code here
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.