public void RemoveChild(TConnectionWay connectionWay, Func <TValue, bool> tester = null) { var connectionWayIndices = ConnectionWays.IndicesOf(connectionWay); var removed = false; foreach (var index in connectionWayIndices.OrderByDescending(x => x)) { if (tester != null && !tester(ChildrenList[index].Value)) { root.GarbageCollect(); return; } ChildrenList.RemoveAt(index); ConnectionWays.RemoveAt(index); removed = true; } if (!removed) { throw new InvalidOperationException(string.Format("Node '{0}' does not have child connected by '{1}'.", Value, connectionWay)); } root.GarbageCollect(); }