using System.Collections.Generic;
public string Name {get; set;}
public Person Father {get; set;}
public Person Mother {get; set;}
public Person (string name, Person father, Person mother) {
public static void Main()
var pebbles = new Person {
Father=new Person { Name="Fred",
Father=new Person { Name="Ed",
Father=new Person { Name="Jim",
Father=new Person { Name="Jack",
Father=new Person { Name="Bo",
Father=new Person { Name="Eutacious", Father=null, Mother=null }, Mother=null },
Mother=new Person { Name="Edna", Father=null, Mother=null } },
Mother=new Person { Name="Wilma",
Mother=new Person { Name="Pearl", Father=null, Mother=null }
List<string> names = getNamesOfAllAncestors(pebbles);
System.Console.WriteLine("[{0}]", string.Join(", ", names));
names = getNamesOfAllAncestors(person:pebbles, maxRecurseLevel:1, includeAnalytics:true);
System.Console.WriteLine("[{0}]", string.Join(", ", names));
public static List<string> getNamesOfAllAncestors(Person person, int recurseLevel=0, int? maxRecurseLevel=null, bool includeAnalytics=false) {
List<string> ancestors = new List<string>();
if ((maxRecurseLevel != null) && (recurseLevel > maxRecurseLevel.Value)) {
ancestors.Add(person.Name + (includeAnalytics?("{" + recurseLevel + "}"):"") );
if(person.Father != null){
ancestors.AddRange(getNamesOfAllAncestors(person.Father, recurseLevel+1, maxRecurseLevel, includeAnalytics));
if(person.Mother != null){
ancestors.AddRange(getNamesOfAllAncestors(person.Mother, recurseLevel+1, maxRecurseLevel, includeAnalytics));