C# Stack

Queue is a class similar to Queue. It add element to the front, and remove element from the front position.

using System.Collections;
Stack s = new Stack();
s.Push("John");
s.Push("Richard");
s.Push("Sophia");

Similarly, Stack do not implement IList, ICollection, so it do not has Add, Remove and Sort methods. It use Pop to read one element from the start position, and at the same time delete it.
s.Pop();
s.Peek(); //read one from the start, but not delete it

Stack generics:
using System.Collections.Generic;
Stack<string> s = new Stack<string>();
s.Push("John");
s.Push("Richard");
s.Push("Sophia");
string str = s.Pop(); //str="Sophia"
s.Count; //2
s.Contains("Sohpia"); //false



endmemo.com © 2017  Terms of Use | Home