using System.Collections.Generic;
public interface IEntity<T>
public interface INode<TId> : IEntity<TId>
string Name { get; set; }
public interface ITreeNode<TId, TItem> : INode<TId>
List<TItem> Children { get; set; }
public record Geolocation : ITreeNode<int, Geolocation>
public int Id { get; set; }
public string Name { get; set; }
public List<Geolocation> Children { get; set; } = new();
public string Type { get; set; }
public Geolocation GetValue(){
return (Geolocation) this;
public record Category : ITreeNode<string, Category>
public string Id { get; set; }
public string Name { get; set; }
public List<Category> Children { get; set; } = new();
public decimal Cost { get; set; }
public Category GetValue(){
public static void Main(string[] args)
List<Category> categories = new()
Children = new List<Category>
Children = new List<Category>
List<Geolocation> locations = new()
Children = new List<Geolocation>
Children = new List<Geolocation>
Category category = TreeFinder("5", categories).GetValue();
Geolocation location = TreeFinder(5, locations).GetValue();
Console.WriteLine(category);
Console.WriteLine(location);
public static ITreeNode<TId, TItem> TreeFinder<TId, TItem>(TId id, IEnumerable<TItem> nodes)
where TItem : class, ITreeNode<TId, TItem>
if(id.Equals(default)) return null;
if(nodes == null) return null;
foreach (ITreeNode<TId, TItem> node in nodes)
ITreeNode<TId, TItem> childNode = TreeFinder(id, node.Children);