fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. public class Test
  6. {
  7. public static void Main()
  8. {
  9. var a = new A
  10. {
  11. bs = new List<B>
  12. {
  13. new B
  14. {
  15. Bs = new List<B>
  16. {
  17. new B
  18. {
  19. Cs = new List<C> {new C(), new C(), new C()}
  20. }
  21. },
  22. Cs = new List<C> {new C(), new C()}
  23. }
  24. }
  25. };
  26.  
  27. Console.WriteLine(a.CountCs());
  28.  
  29. Console.ReadLine();
  30.  
  31. }
  32. }
  33.  
  34. public class A
  35. {
  36. public List<B> bs { get; set; }
  37.  
  38. public int CountCs()
  39. {
  40. return bs.Sum(b => b.TotalCs());
  41. }
  42. }
  43.  
  44. public class B
  45. {
  46. public List<B> Bs { get; set; }
  47. public List<C> Cs { get; set; }
  48.  
  49. public int TotalCs()
  50. {
  51. var subtotal = Bs != null ? Bs.Sum(b => b.TotalCs()) : 0;
  52.  
  53. return subtotal + Cs.Count;
  54. }
  55. }
  56.  
  57. public class C
  58. {
  59.  
  60. }
  61.  
Success #stdin #stdout 0.04s 24328KB
stdin
Standard input is empty
stdout
5