Use the generic Stack<T>
type instead of Stack
, that way you will get a stack of the specific type, and you don’t have to cast the values that you read from it:
Stack<string> st = new Stack<string>();
st.Push("joginder");
st.Push("singh");
st.Push("banger");
st.Push("Kaithal");
Now when you pop something from the stack, or loop through the items, it will be a string already, and you don’t have to cast it.