.Net application development specialists
asp.net, c#, vb.net, html, javascript, jquery, html, xhtml, css, oop, design patterns, sql server, mvc and much more
contact: admin@paxium.co.uk

Paxium is the company owned by myself, Dave Amour and used for providing IT contract development services including


  • Application development - Desktop, Web, Services - with Classic ASP, Asp.net WebForms, Asp.net MVC, Asp.net Core
  • Html, Css, JavaScript, jQuery, React, C#, SQL Server, Ado.net, Entity Framework, NHibernate, TDD, WebApi, GIT, IIS
  • Database schema design, implementation & ETL activities
  • Website design and hosting including email hosting
  • Training - typically one to one sessions
  • Reverse Engineering and documentation of undocumented systems
  • Code Reviews
  • Performance Tuning
  • Located in Cannock, Staffordshire
Rugeley Chess Club Buying Butler Cuckooland Katmaid Pet Sitting Services Roland Garros 60 60 Golf cement Technical Conformity Goofy MaggieBears Vacc Track Find Your Smart Phone eBate Taylors Poultry Services Lafarge Rebates System Codemasters Grid Game eBate DOFF

Collection Classes in .net - the Stack

This is the sixth part of an article on collection classes in .net with C#.  This part covers the Stack.

For the first part see http://www.audacs.co.uk/ViewPage.aspx?PageID=512

For the next part on the Queue see http://www.audacs.co.uk/ViewPage.aspx?PageID=519

The stack is a collection class of course and as it's name suggests works like a stack of objects.  A useful analogy is a stack of plates.  The last one placed on the stack is the first one to come off.  Items are not added and removed from a stack usinng Add and Remove methods. Instead we use Push and Pop methods.

There are actually 2 stacks within the .net framework.  The first one is the original stack and the second one is the Generic version.  These are found as follows:

System.Collections.Stack
System.Collections.Generic.Stack

The use of both of these is pretty much the same other than one is strongly typed at compile time.

The following code shows a sample usage first with the original Stack and then with the generic one.  The output is of course the same fro both programs.

Original Stack

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace Stack
{
    class GenericStack
    {
        static void Main()
        {
            System.Collections.Generic.Stack<string> films = new Stack<string>();
 
            films.Push("Dead Poets Society");
            films.Push("The good, the bad and the ugly");
            films.Push("An unfinished life");
            films.Push("Laurence of Arabia");
            films.Push("The Matrix");
 
            foreach (string film in films)
            {
                Console.WriteLine(film);
            }
 
            string poppedFilm = (string)films.Pop();
 
            Console.WriteLine();
 
            Console.WriteLine("popped film: " + poppedFilm);
 
            Console.Read();
        }
    }
}


Generic Stack

using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
 
namespace Stack
{
    class Program
    {
        static void Main(string[] args)
        {
            System.Collections.Stack films = new System.Collections.Stack();
 
            films.Push("Dead Poets Society");
            films.Push("The good, the bad and the ugly");
            films.Push("An unfinished life");
            films.Push("Laurence of Arabia");
            films.Push("The Matrix");
 
            foreach (string film in films)
            {
                Console.WriteLine(film);
            }
 
            string poppedFilm = films.Pop();
 
            Console.WriteLine();
           
            Console.WriteLine("popped film: " + poppedFilm);
 
            Console.Read();
        }
    }
}
 


Program Output

 

Note that in the generic version then on the following line we don't need to cast to a string like we do in the originall version.

string poppedFilm = films.Pop();


So that's really all there is to the Stack.  Its real purpose is to make it easy to model solutions where the thing your are modelling with software works like a stack.

Finally you should be aware that a stack is often referred to as a LIFO structure where LIFO stands for Last In First Out.

For the next part on the Queue see http://www.audacs.co.uk/ViewPage.aspx?PageID=519