C# Collections Part 4: Stack

Stack:

It represents a last-in, first out collection of object. It is used when you need a last-in, first-out access of items. When you add an item in the list, it is called pushing the item and when you remove it, it is called popping the item.

Code:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using System.Collections;  
  7.   
  8. namespace sortedList  
  9. {  
  10.     class Program  
  11.     {  
  12.         static void Main(string[] args)  
  13.         {  
  14.   
  15.             Stack st = new Stack();  
  16.             st.Push("Sujan");  
  17.             st.Push(003);  
  18.             st.Push("Shakil");  
  19.             st.Push("Shimul");  
  20.             st.Push(01);  
  21.             st.Push(02);  
  22.             sprint(st);  
  23.             st.Pop();  
  24.             st.Pop();  
  25.             sprint(st);  
  26.   
  27.             Console.ReadKey();  
  28.         }  
  29.   
  30.   
  31.         public static void sprint(Stack stack)  
  32.         {  
  33.             foreach (var i in stack)  
  34.             {  
  35.                 Console.WriteLine(i);  
  36.             }  
  37.         }  
  38.     }  
  39. }  
Share:

0 Comments:

Post a Comment