123
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace StackGenerics { class Program { static void Main(string[] args) { UseGenericStack(); Console.ReadLine(); } static void UseGenericStack() { Stack<Person> stackOfPeople = new Stack<Person>(); // filling stack stackOfPeople.Push(new Person { Age = 123, FirstName = "123", LastName = "123" }); stackOfPeople.Push(new Person { Age = 1234, FirstName = "1234", LastName = "1234" }); stackOfPeople.Push(new Person { Age = 12345, FirstName = "12345", LastName = "12345" }); Console.WriteLine("Count stack of people {0}", stackOfPeople.Count); //getting top element and pop it Person person = stackOfPeople.Peek(); //do something with person person.Age = 12; // for example stackOfPeople.Pop(); // get it away from the stack top //next person = stackOfPeople.Peek(); //do something with person person.Age = 12; // for example stackOfPeople.Pop(); // get it away from the stack top // next person = stackOfPeople.Peek(); //do something with person person.Age = 12; // for example stackOfPeople.Pop(); // get it away from the stack top Console.WriteLine("some job done"); Console.WriteLine("Count stack of people {0}", stackOfPeople.Count); } } public class Person { public int Age { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public Person() { } public Person(int age, string firstName, string lastName) { Age = age; FirstName = firstName; LastName = lastName; } } } |