Using objects with a stack in c#

Person Class:

 public class person { string firstName; string lastName; public person(string _firstName, string _lastName) { firstName = _firstName; lastName = _lastName; } override public string ToString() { return firstName + " " + lastName; } }

adding the

person class object to a stack

 Stack s = new Stack(); s.Push(new person("p1fn", "p1ln")); s.Push(new person("p2fn", "p2ln")); s.Push(new person("p3fn", "p3ln")); Console.WriteLine("Stack demonstration:"); for (int i = 1; i <= 3; i++) Console.WriteLine(s.Pop().ToString());

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.