usingSystem;usingSystem.Collections.Generic;namespaceCSharp Stack {classProgram {Static voidMain (string[] args) {Stack<string> stack =Newstack<string>(); Stack. Push ("element 1"); Stack. Push ("element 2"); Stack. Push ("element 3"); Stack. Push ("element 4"); //traversing elements foreach(stringIteminchstack) {Console.WriteLine (item); } //Pop popup element to remove and return the object at the top of the Stack. Console.WriteLine ("\ nto Remove and return the object at the top of the Stack---{0}", Stack. Pop ()); //Peek returns the object at the top of the Stack without removing it. Console.WriteLine ("returns the object at the top of the Stack without removing it---{0}", Stack. Peek ()); foreach(stringIteminchstack) {Console.WriteLine (item); } //Create a new stack, copy elementsstack<string> stacknew =Newstack<string>(Stack. ToArray ());Console.WriteLine ("\ n Create a new stack and copy the elements:"); foreach(stringIteminchstacknew) {Console.WriteLine (item); } //contains usageConsole.WriteLine ("\nstacknew.contains (\ "Element 3\") = {0}", Stacknew.contains (" Four")); Console.WriteLine ("\nstacknew.clear ()"); Stacknew.clear (); Console.WriteLine ("\nstacknew.count = {0}", Stacknew.count); Console.readkey (); } }}
Use of C # stack stack