Posted By

sureshdasari on 10/08/18


Tagged

csharp programming dotnet visualStudio tutlane


Versions (?)

C# Stack with Examples


 / Published in: C#
 

URL: https://www.tutlane.com/tutorial/csharp/csharp-stack

In c#, Stack is used to represent a collection of objects which stores an elements in LIFO (Last in, First out) style i.e. the element which added last will be the first to come out.

  1. using System;
  2.  
  3. using System.Collections;
  4.  
  5.  
  6.  
  7. namespace Tutlane
  8.  
  9. {
  10.  
  11. class Program
  12.  
  13. {
  14.  
  15. static void Main(string[] args)
  16.  
  17. {
  18.  
  19. // Create and initialize a stack
  20.  
  21. Stack stk = new Stack();
  22.  
  23. stk.Push("Welcome");
  24.  
  25. stk.Push("Tutlane");
  26.  
  27. stk.Push(20.5f);
  28.  
  29. stk.Push(10);
  30.  
  31. stk.Push(null);
  32.  
  33. stk.Push(100);
  34.  
  35. Console.WriteLine("Number of Elements in Stack: {0}", stk.Count);
  36.  
  37. Console.WriteLine("******Stack Elements******");
  38.  
  39. // Access Stack Elements
  40.  
  41. foreach (var item in stk)
  42.  
  43. {
  44.  
  45. Console.WriteLine(item);
  46.  
  47. }
  48.  
  49. Console.ReadLine();
  50.  
  51. }
  52.  
  53. }
  54.  
  55. }

Report this snippet  

You need to login to post a comment.