using System.Collections;
using System.Collections.Generic;
public static void Main()
var categoryTree = new List<Category>();
categoryTree.Add(new Category { Name = "Parent1"});
categoryTree.Last().Children.Add(new Category { Name = "Child1OfParent1" });
categoryTree.Last().Children.Add(new Category { Name = "Child2OfParent1" });
categoryTree.Last().Children.Add(new Category { Name = "Child3OfParentz" });
Console.WriteLine("source list of children: " + string.Join(",", categoryTree.Last().Children.Select(x => x.Name)));
var changed = RemoveMatchingChildren(categoryTree, "Parent1");
Console.WriteLine("after modification");
Console.WriteLine("source list of children: " + string.Join(",", categoryTree.Last().Children.Select(x => x.Name)));
public static List<Category> RemoveMatchingChildren(List<Category> categories, string nameParam)
Console.WriteLine("~~removing matching child categories~~");
var filtered = categories;
filtered.ForEach(parent =>
for(var i = parent.Children.Count; i > 0; i--)
if(parent.Children[childIndex].Name.Contains(nameParam))
parent.Children.RemoveAt(childIndex);
Console.WriteLine("list of children after modification: " + string.Join(",", filtered.Last().Children.Select(x => x.Name)));
public static List<Category> ModifyChildren(List<Category> categories)
Console.WriteLine("~~modifying categories~~");
var filtered = categories;
filtered.ForEach(category =>
category.Children = new List<Category>();
category.Children.Add(new Category { Name = "I'm the destructor of the world" });
Console.WriteLine("list of children after modification: " + string.Join(",", filtered.Last().Children.Select(x => x.Name)));
public string Name {get;set;}
public List<Category> Children {get;set;} = new List<Category>();