LIFO collections with Stack of T in .NET C#

LIFO, that is last-in-first-out, collections are represented by the generic Stack of T class in .NET. Stacks are collections where a new element is placed on top of the collection and is removed first when an item is being retrieved. Hence the item that was entered first will get to stay longest in the stack.

Let’s say that you’re throwing a party where you follow a Stack policy as far as guests are concerned. As time goes by you’d like all of them to leave eventually and the first one to go will be the last person who has arrived. This might not be a very fair way to treat your guests but there you go.

First some terminology:

  • Putting an item on top of the stack is called pushing
  • Removing an item from the top of the stack is called popping
  • Looking at the topmost element in the stack without removing it is called peeking

The 3 most important methods of Stack are consequently named Push, Pop and Peek that do exactly as it says in the above list.

Consider the following Guest class:

public class Guest
{
	public string Name { get; set; }
	public int Age { get; set; }
}

The following code example demonstrates all 3 methods:

Stack<Guest> guests = new Stack<Guest>();
guests.Push(new Guest() { Age = 25, Name = "John" });
guests.Push(new Guest() { Age = 24, Name = "Barbara" });
guests.Push(new Guest() { Age = 24, Name = "Phil" });
guests.Push(new Guest() { Age = 23, Name = "Fred" });
guests.Push(new Guest() { Age = 26, Name = "Hannah" });
guests.Push(new Guest() { Age = 27, Name = "Cindy" });

Debug.WriteLine("Full guest list on stack:");

foreach (Guest guest in guests)
{
	Debug.WriteLine("{0}, {1}", guest.Name, guest.Age);
}

Debug.WriteLine("");

Guest firstToLeave = guests.Pop();
Debug.WriteLine(string.Format("First to leave: {0}", firstToLeave.Name));

Debug.WriteLine("Guest list after the first goodbye:");
foreach (Guest guest in guests)
{
	Debug.WriteLine("{0}, {1}", guest.Name, guest.Age);
}
			
Guest soonToLeave = guests.Peek();
Debug.WriteLine(string.Format("Just checking who'll leave next: {0}", soonToLeave.Name));

This produces the following output:

Full guest list on stack:
Cindy, 27
Hannah, 26
Fred, 23
Phil, 24
Barbara, 24
John, 25

First to leave: Cindy
Guest list after the first goodbye:
Hannah, 26
Fred, 23
Phil, 24
Barbara, 24
John, 25
Just checking who’ll leave next: Hannah

View all various C# language feature related posts here.

Advertisement

About Andras Nemes
I'm a .NET/Java developer living and working in Stockholm, Sweden.

One Response to LIFO collections with Stack of T in .NET C#

  1. Pingback: Summary of thread-safe collections in .NET – .NET training with Jead

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Elliot Balynn's Blog

A directory of wonderful thoughts

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

WEB APPLICATION DEVELOPMENT TUTORIALS WITH OPEN-SOURCE PROJECTS

Once Upon a Camayoc

Bite-size insight on Cyber Security for the not too technical.

%d bloggers like this: