-5

I have the below class and I need it to have a child and child's child within the parent class. How can I do this?

public class Person  {
public string Name { get; set; }
public int Age { get; set; }
// this person can have one or more children with Name & age properties
//this person's children can have one or more children with Name & age properties
}  

Thank you

shw
  • 135
  • 3
  • 12
  • 2
    `public List Children { get; } = new List();` More generally, this is a kind of **[recursive data type](https://en.wikipedia.org/wiki/Recursive_data_type)** – p.s.w.g Aug 04 '16 at 15:08
  • 2
    `public List Children { get;set;}`? – GSerg Aug 04 '16 at 15:08
  • Some Monitor won't let me answer your question. Don't create a list of Children because each Child would then only have one Parent. Each Person should have a List of Parents... List Parents. – Jason Williams Aug 04 '16 at 15:19
  • @JasonWilliams Then how would you handle one parent having multiple children? Apparently this parent will then need to appear on multiple lists, belonging to each of their children? And if you accept this is [possible in principle](http://stackoverflow.com/a/13293127/11683), then why can't a Child appear on multiple lists, one for each Parent? – GSerg Aug 04 '16 at 15:28
  • It is an inverse tree. In practice, People are not considered Children once they've had their own. So, when implementing this tree structure, you would have a List where there is only one instance of each Person. However, there is an Object Reference on each Person indicating who their Parent is. – Jason Williams Aug 04 '16 at 15:35
  • 1
    @JasonWilliams IMO, it depends on exactly what your use case is. With a list of children, it's difficult to get a list of ancestors; with a list of parents, it's difficult to get a list of children or siblings. (in both cases you need to search through a global list of all people). OP really hasn't provided enough information to know what's the best model. And of course, using either structure, there's no way to avoid 'nonsense' scenarios (e.g. a cyclical parent-child relationship). You really need a **[more sophisticated data structure](https://en.wikipedia.org/wiki/Directed_acyclic_graph)**. – p.s.w.g Aug 04 '16 at 15:51
  • @JasonWilliams `In practice, People are not considered Children once they've had their own` - in practice people are considered and treated as children by their parents for the rest of their lives, regardless of how miserable that makes them feel. However, bringing the discussion from programming grounds to real world interactions between actual people might suggest that you completely misunderstood my point. My point was that `Don't create a list of Children because each Child would then only have one Parent` is wrong, and your suggested fix does not in fact change anything at all. – GSerg Aug 04 '16 at 16:48
  • In my specific case every child can only have one parent, so the below works perfectly. Thanks for all your help! – shw Aug 05 '16 at 00:21

1 Answers1

1

It can reference itself.

public class Person  {
    public string Name { get; set; }
    public int Age { get; set; }
    public List<Person> Children { get; set; }
}  
deadwards
  • 2,109
  • 1
  • 16
  • 27
  • Personally, I would prefer making `Children` readonly and initializing it with an empty list. That way you never have to worry about checking if `person.Children == null`. – p.s.w.g Aug 04 '16 at 15:16
  • Don't create a list of Children because each Child would then only have one Parent. Each Person should have a List of Parents... List Parents – Jason Williams Aug 04 '16 at 15:20
  • Thank you. That's exactly what I need – shw Aug 04 '16 at 15:26