shw shw - 3 months ago 9
C# Question

How to have a property within a class with the same properties as the parent class?

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

Answer

It can reference itself.

public class Person  {
    public string Name { get; set; }
    public int Age { get; set; }
    public List<Person> Children { get; set; }
}