Here, I will talk about Stack<T>. Basically, Stack works on the principle of Last In First Out (LIFO). That means that of the elements inserted into the stack, the element inserted last will come out first. Here, there are two fundamental principles involved, 1) Push and 2) Pop. Push is to insert the element onto the stack and Pop to remove it.
In the following snippet I have explained all the basic operations involved with Stack.
- using System.Collections;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Linq;
- using System;
- namespace Collections
- {
- internal class Stack
- {
- private static void Main(string[] args)
- {
- var monthsofYear = new Stack < string > ();
- monthsofYear.Push("December");
- monthsofYear.Push("November");
- monthsofYear.Push("October");
- monthsofYear.Push("September");
- monthsofYear.Push("August");
- monthsofYear.Push("July");
- monthsofYear.Push("June");
- monthsofYear.Push("May");
- monthsofYear.Push("April");
- monthsofYear.Push("March");
- monthsofYear.Push("February");
- monthsofYear.Push("January");
- //Peek returns top element without removing it
- string topElement = monthsofYear.Peek();
- Console.WriteLine("Top element is: {0}", topElement);
- foreach(var month in monthsofYear)
- {
- Console.WriteLine(month);
- }
- //removed the top element
- string removeditem = monthsofYear.Pop();
- Console.WriteLine("Collection after removing: {0}", removeditem);
- foreach(var month in monthsofYear)
- {
- Console.WriteLine(month);
- }
- Console.ReadLine();
- }
- }
- }
- using System.Collections;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Linq;
- using System;
- namespace Collections
- {
- internal class Queues
- {
- private static void Main(string[] args)
- {
- var monthsofYear = new Queue < string > ();
- monthsofYear.Enqueue("January");
- monthsofYear.Enqueue("February");
- monthsofYear.Enqueue("March");
- monthsofYear.Enqueue("April");
- monthsofYear.Enqueue("May");
- monthsofYear.Enqueue("June");
- monthsofYear.Enqueue("July");
- monthsofYear.Enqueue("August");
- monthsofYear.Enqueue("September");
- monthsofYear.Enqueue("October");
- monthsofYear.Enqueue("November");
- monthsofYear.Enqueue("December");
- //Peek returns top element without removing it
- string topElement = monthsofYear.Peek();
- Console.WriteLine("Top element is: {0}", topElement);
- foreach(var month in monthsofYear)
- {
- Console.WriteLine(month);
- }
- string removedItem = monthsofYear.Dequeue();
- Console.WriteLine("Collection after removing: {0}", removedItem);
- foreach(var month in monthsofYear)
- {
- Console.WriteLine(month);
- }
- Console.ReadLine();
- }
- }
- }

Mohammad KhalidPosted Jan 23, 2018, 11:06 PM
Very good article. very simple explanation. We have been studying this since our college days. Still we need to refer them. Please keep writing these sort of articles .
Santhakumar MunuswamyPosted Jul 11, 2015, 2:59 AM
Nice article! Thanks for sharing
Rizwan AshrafPosted Jul 10, 2015, 10:18 AM
Thanks Bro !
Narasimha Reddy ChennupalliPosted Jul 10, 2015, 9:54 AM
Good one..
Debasis SahaPosted Jul 9, 2015, 3:25 AM
Good One..
Vignesh ManiPosted Jul 9, 2015, 3:12 AM
Nice
RakeshPosted Jul 9, 2015, 2:45 AM
Good one