简体   繁体   中英

Class that has a list of itself as a property

I have the following class and wondered if it is allowed?

     public class Node
     {
        public string Type { get; set; }
        public string Id { get; set; }

        public string Text { get; set; }

        public string ParentNodeId { get; set; }

        public List<Node> TargetNodes { get; set; }
     }

The code compiles and even works but I am getting stack errors where I shouldn't be getting stack errors and wondered if it is because of my TargetNodes property? Should I create a new class for TargetNodes?

The problem was unrelated. I realised the function I was using to populate the nodes was calling itself and creating endless nodes in the list finding new targets and entering a loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM