fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace doglist
  6. {
  7.  
  8. public class DogClass
  9. {
  10. private static int iddog;
  11. public int Iddog { get; set; }
  12. public string Name { get; set; }
  13. public string Breed { get; set; }
  14. public bool IsBitch { get; set; }
  15. public int Age { get; set; }
  16.  
  17. public DogClass()
  18. {
  19. Iddog = iddog++;
  20. }
  21.  
  22. // static constructor to set isddog
  23. static DogClass()
  24. {
  25. iddog = 0;
  26. }
  27.  
  28. public override string ToString()
  29. {
  30. return Name+'('+Iddog+") "+Breed + (IsBitch ?" B ":" D ") + "Age="+Age;
  31. }
  32. }
  33.  
  34. public class DogList
  35. {
  36. private readonly List<DogClass> dogs;
  37. public List<DogClass> Dogs
  38. {
  39. get { return dogs; }
  40. }
  41.  
  42. public int Count
  43. {
  44. get
  45. {
  46. return dogs.Count;
  47. }
  48. }
  49.  
  50. public DogClass this[int i]
  51. {
  52. get
  53. {
  54. return dogs[i];
  55. }
  56. }
  57.  
  58. public void AddDog(DogClass dog)
  59. {
  60. dogs.Add(dog);
  61. }
  62.  
  63. public void RemoveDog(DogClass dog)
  64. {
  65. dogs.Remove(dog);
  66. }
  67.  
  68. public DogClass FindDog(int iddog)
  69. {
  70. return dogs.FirstOrDefault(Dog => Dog.Iddog == iddog);
  71. }
  72.  
  73. public DogList()
  74. {
  75. dogs = new List<DogClass> {};
  76. dogs.Capacity = 20;
  77. }
  78.  
  79. public void ClearList()
  80. {
  81. dogs.Clear();
  82. }
  83.  
  84. // LINQ version of
  85. // foreach (var d in dogs)
  86. // yield return d;
  87. public IEnumerator<DogClass> GetEnumerator()
  88. {
  89. return ((IEnumerable<DogClass>) dogs).GetEnumerator();
  90. }
  91.  
  92. public int MySortFunction(DogClass dog1, DogClass dog2)
  93. {
  94. return dog1.Name.CompareTo(dog2.Name);
  95. }
  96.  
  97. public void Sort()
  98. {
  99. dogs.Sort(MySortFunction);
  100. }
  101. }
  102.  
  103.  
  104.  
  105. class Program
  106. {
  107. static void Main(string[] args)
  108. {
  109. var dogs = new DogList();
  110. dogs.AddDog(new DogClass(){Name=@"Rufus",Breed="Minature Poodle",IsBitch = false,Age=4});
  111. dogs.AddDog(new DogClass() {Name = @"Monty", Breed = "Minature Poodle", IsBitch = false,Age=5 });
  112. dogs.AddDog(new DogClass() { Name = @"Sam", Breed = "Standard Poodle", IsBitch = false,Age=12 });
  113. dogs.AddDog(new DogClass() { Name = @"Judy", Breed = "Afghanistan", IsBitch = true,Age=3 });
  114.  
  115. var dog = dogs.FindDog(2);
  116. Console.WriteLine("Dog 2 is {0}",dog);
  117. Console.WriteLine("There are {0} dogs",dogs.Count);
  118.  
  119. Console.WriteLine("Dog[1] is {0}", dogs[1]);
  120. //dogs.Dogs.RemoveAll(adog => adog.IsBitch == false);
  121.  
  122. var averagAge = 0;
  123. foreach (var adog in dogs)
  124. {
  125. averagAge += adog.Age;
  126. }
  127. Console.WriteLine("Average age is {0}", averagAge/dogs.Count);
  128.  
  129. dogs.Sort();
  130.  
  131. Console.WriteLine("Male Dogs in alphaberic order by name");
  132. foreach (var dog1 in dogs)
  133. {
  134. if (!dog1.IsBitch)
  135. {
  136. Console.WriteLine(dog1);
  137. }
  138. }
  139. Console.ReadKey();
  140. }
  141. }
  142. }
  143.  
Success #stdin #stdout 0.07s 34096KB
stdin
Standard input is empty
stdout
Dog 2 is Sam(2) Standard Poodle D Age=12
There are 4 dogs
Dog[1] is Monty(1) Minature Poodle D Age=5
Average age is 6
Male Dogs in alphaberic order by name
Monty(1) Minature Poodle D Age=5
Rufus(0) Minature Poodle D Age=4
Sam(2) Standard Poodle D Age=12