Solutions
Exercise 1
Steve implemented a sorting function for towers using a delegate:
public class Tower { public string Name { get; set; } public int Damage { get; set; } } public delegate int CompareTowers(Tower a, Tower b); public static void SortTowers(List<Tower> towers, CompareTowers compare) { towers.Sort((x, y) => compare(x, y)); } // Usage: List<Tower> towers = new List<Tower> { new Tower { Name = "Archer", Damage = 10 }, new Tower { Name = "Cannon", Damage = 20 }, new Tower { Name = "Mage", Damage = 15 } }; SortTowers(towers, (a, b) => b.Damage.CompareTo(a.Damage)); // Sort descending foreach (var tower in towers) { Console.WriteLine($"{tower.Name}: {tower.Damage} damage"); }
This solution creates...