class GraphicObject { public virtual string GroupName { get; set; }= "Group"; public string Color; private Lazy<List<GraphicObject>> graphicObjects = new Lazy<List<GraphicObject>>(); public List<GraphicObject> GraphicObjects => graphicObjects.Value; public override string ToString() { var sb = new StringBuilder(); Print(sb, 0); return sb.ToString(); } private void Print(StringBuilder sb, int v) { sb.AppendLine($"{new string('-', v)} GroupName: {this.GroupName} Color:{Color}"); foreach (var item in GraphicObjects) { item.Print(sb, v + 1); } } } class Circle:GraphicObject { public override string GroupName => "Circle"; } class Square: GraphicObject { public override string GroupName => "Square"; } class Program { static void Main(string[] args) { GraphicObject graphic1 = new GraphicObject(); Circle circle1 = new Circle(); circle1.Color = "Yellor"; Circle circle2 = new Circle(); circle2.Color = "Yellor"; graphic1.GraphicObjects.Add(circle1); graphic1.GraphicObjects.Add(circle2); Console.WriteLine("1. "+graphic1.ToString()); GraphicObject graphic2 = new GraphicObject(); graphic2.GroupName = "Square"; Square s1 = new Square(); s1.Color = "blue"; Square s2 = new Square(); s2.Color = "blue"; graphic2.GraphicObjects.Add(s1); graphic2.GraphicObjects.Add(s2); Console.WriteLine("2. "+graphic2.ToString()); } }