Initialize a List<string> from a Stack<string> - CSharp Collection

CSharp examples for Collection:List

Description

Initialize a List<string> from a Stack<string>

Demo Code

using System;/* w w w .  j  a v a 2  s .  c  o m*/
using System.Collections.Generic;
class Program
{
   static void Main(string[] args)
   {
      Console.WriteLine("\nInitialize a List<string> from a Stack<string>: ");
      Stack<string> stack = new Stack<string>();
      stack.Push("one");  // To add to a stack, Push the item (ends up on the top).
      stack.Push("two");
      stack.Push("three");
      string topItemOnStack = stack.Pop();  // Remove by Popping the stack.
      // Add the stack to a List.
      List<string> strList = new List<string>(stack);
      Console.WriteLine("Resulting list: ");
      foreach (string s in strList)
      {
         Console.WriteLine(s);
      }
   }
}

Result


Related Tutorials