using System.Text.RegularExpressions;
using System.Collections.Generic;
public class Subfoo : IHasId {
public int Id { get; set;}
public List<T> Children {get; internal set;}
Children = new List<T>();
public TItem GetById<TItem>(int id)
return Children.Where(t => t is TItem).Cast<TItem>().FirstOrDefault(t => t.Id == id);
public static void Main()
var foo = new Foo<Subfoo>();
foo.Children.Add(new Subfoo(23));
Console.WriteLine(foo.GetById<Subfoo>(23) == null ? "Null" : "Found");