Поиск элементов в Stack - C#
Формулировка задачи:
Поиск элементов с заданным значением третьего поля, немогу в Stack зделать. Помогите
class Building { public int length; public int weigth; public int height; public Building(int length, int weigth, int height) { this.length = length; this.weigth = weigth; this.height = height; } }
class Program { static void Main(string[] args) { StackCollection = new Stack (); while (true) { Console.Clear(); Console.WriteLine("What do you want to do? \n1)Add new building \n2)Delete building \n3)Find \n4)Show building \n5)Exit"); var key = Console.ReadKey(); switch (key.Key) { case ConsoleKey.D1: Add(Collection); break; case ConsoleKey.D2: Delete(Collection); break; //case ConsoleKey.D3: // Find(Collection); //break; case ConsoleKey.D4: Show(Collection); break; case ConsoleKey.D5: Environment.Exit(1); break; } } } static void Add(Stack Collection) { Console.Clear(); try { Console.WriteLine("Please enter the length of the building"); int length = int.Parse(Console.ReadLine()); Console.WriteLine("Please enter the wigth:"); int weigth = int.Parse(Console.ReadLine()); Console.WriteLine("Please enter height:"); int height = int.Parse(Console.ReadLine()); Collection.Push(new Building(length, weigth, height)); } catch (FormatException) { Console.WriteLine("Format exception!!!"); } Console.WriteLine("The new building was added to the collection"); Console.ReadKey(); } static void Delete(Stack Collection) { Console.Clear(); Collection.Clear(); Console.WriteLine("Were deleted from the collection"); Console.ReadKey(); } static void Show(Stack Collection) { Console.Clear(); foreach (Building c in Collection) { Console.WriteLine("Bulinding length: {0} \nBuilding weigth: {1} \nBulding height: {2}", c.length, c.weigth, c.height); Console.WriteLine(); } Console.ReadKey(); }
Не могу зделать метод поиска
Решение задачи: «Поиск элементов в Stack»
textual
Листинг программы
static void Find(Stack<Building> Collection) { foreach (Building b in Collection) { if (b.height == искомое_значение) { // Нашли! } } }