Convert Stack into String in C#

Hi Programmers , Welcome to new Article of Ozanecare. This article i am going to explain Convert Stack into String in c#.net. In c# we should use Stack class , It is based on the principle of LIFO (Last in First Out). The two important methods used push and pop. the push method pushes an element on the top of the stack and with the help of ToString method we can easily convert stack elements into string. see the below coding.

using System;
using System.Collections;

namespace ConsoleApp5
{
    class Program
    {
        static void Main(string[] args)
        {
            Stack st = new Stack();
            st.Push("First");
            st.Push(1);
            st.Push(2);
            st.Push(3);
            st.Push(4);
            st.Push(5);
            Console.WriteLine("Stack Elements ..... ");
            foreach (var v1 in st)
                Console.WriteLine(v1);

            Console.WriteLine(" ********************** ");
            foreach (object str in st)
            {
                String s = str.ToString();
                Console.WriteLine(s);
            }
            Console.WriteLine("The type of string after To 
                 String   Method :" + st.ToString().GetType());
        }
    }
}

Code Screenshot with output screen.see below image.

Happy Coding…Thanks.

Post Author: adama