Home Basics C# Stack

C# Stack

A Stack is a type of collection that stores elements in Last In First Out style. C# includes the generic Stack<T> and non-generic Stack collection classes

You can use the Push() method and add elements to the stack.
The Pop() method can be used to remove and return a topmost element from the stack.
The Peek() method will always return the last inserted element of the stack, and it won’t delete any element from the stack.

Stack Properties

The following are some of the commonly used properties of a stack.

Property Description
Count This will return the total number of elements in a stack.
IsSynchronized This is used to get a value to indicate that access to the stack is synchronized or not.

Stack Methods

The following are some of the commonly used stack methods to perform operations like add, delete, etc., on elements of a stack.

Method Description
Push This is used to insert an object at the top of a stack.
Pop This will remove and return an object at the top of the stack.
Clear This will remove all the elements from the stack.
Clone This will create a shallow copy of the stack.
Contains This is used to determine whether an element exists in a stack or not.
Peek This is used to return a top element from the stack.

Create a Stack

You can create an object of the Stack<T> by specifying a type parameter for the type of elements it can store.

In this example we create a Stack and use the Push method to add elements. We then use a foreach loop to display the items

using System;
using System.Collections.Generic;

public class Program
{
	public static void Main()
	{
	Stack<int> lottery  = new Stack<int>();
	lottery.Push(6);
	lottery.Push(24);
	lottery.Push(19);
	lottery.Push(41);
	lottery.Push(12);
	lottery.Push(32);
		
	foreach (var item in lottery)
       Console.Write(item + ","); //prints 4,3,2,1, 

	}
}

Run this and you will see the following

32,12,41,19,24,6,

Pop() Method

the stack Pop() method will always remove and return a top element of the queue. Here is an example.

using System;
using System.Collections.Generic;

public class Program
{
	public static void Main()
	{
		Stack<int> lottery  = new Stack<int>();
		lottery.Push(6);
		lottery.Push(24);
		lottery.Push(19);
		lottery.Push(41);
		lottery.Push(12);
		lottery.Push(32);

		Console.WriteLine("Number of Elements : {0}", lottery.Count);
		Console.WriteLine("******Stack Elements******");
		// Access Elements
		while (lottery.Count > 0)
		{
			Console.WriteLine(lottery.Pop());
		}
		Console.WriteLine("Number of Elements : {0}", lottery.Count);
		Console.ReadLine();
	}
}

Run this and you will see the following

Number of Elements : 6
******Stack Elements******
32
12
41
19
24
6
Number of Elements : 0

Peek() Method

the Peek() method will always return the last inserted element of the stack. Here is an example.

using System;
using System.Collections.Generic;

public class Program
{
	public static void Main()
	{
		Stack<int> lottery  = new Stack<int>();
		lottery.Push(6);
		lottery.Push(24);
		lottery.Push(19);
		lottery.Push(41);
		lottery.Push(12);
		lottery.Push(32);

		Console.WriteLine("Number of Elements : {0}", lottery.Count);
		Console.WriteLine("******Stack Elements******");
		// Access Elements
		Console.WriteLine(lottery.Peek());
		Console.WriteLine("Number of Elements : {0}", lottery.Count);
		Console.ReadLine();
	}
}

Run this and you will see this

Number of Elements : 6
******Stack Elements******
32
Number of Elements : 6

 

Contains() method

We can use the Contains() method to check whether an element exists in a stack or not. If the element is found in the stack, it will return true otherwise false.

using System;
using System.Collections.Generic;

public class Program
{
	public static void Main()
	{
		Stack<int> lottery  = new Stack<int>();
		lottery.Push(6);
		lottery.Push(24);
		lottery.Push(19);
		lottery.Push(41);
		lottery.Push(12);
		lottery.Push(32);

		Console.WriteLine("******Stack Example******");
		// Access Elements
		Console.WriteLine("Contains Element 6: {0}", lottery.Contains(6));
        Console.WriteLine("Contains Element 7: {0}", lottery.Contains(7));

		Console.ReadLine();
	}
}

Run the following

******Stack Example******
Contains Element 6: True
Contains Element 7: False

 

You may also like