Advertisement



< Prev
Next >



C# Stack<T> Class





Stack<T> is a generic collection class which is used to maintain a last-in, first-out(LIFO) collection of same type of objects i.e. the last element to enter the Stack<T> is the first one to be removed. In other words, Stack<T> can be thought of as a pile of dishes, a new dish is always added to the top of the pile and the first dish to be removed is from the top as well.

Note: Stack<T>. It implements the ICollection, IEnumerable, and IEnumerable<T> interfaces.







Important features of Stack<T>







Constructors of Stack<T>


Constructor Description
Stack<T>()
This constructor creates an empty instance of the Stack<T> class by using the default initial capacity.

The capacity of a Stack<T> is the number of elements it can hold, as the new elements are added to the Stack<T>, its capacity is automatically increased.

Stack<T>(int capacity)
This constructor initializes created a Stack<T> that is empty and has the specified initial capacity..

Stack<T>(IEnumerable<T>)
This constructor creates a Stack<T> initialized with the elements of an IEnumerable<T>.






Properties of Stack<T>:


Properties Description
Count
This property gives total number of elements in a Stack<T>.

IsSynchronzied
This property determines whether an ICollection is thread-safe or not.

SyncRoot
Thie property gets an object that can be used to synchronize access to the ICollection






Some commonly used methods of Stack<T> class


Methods Description
T Pop()
This method removes and returns the object at the top of the Stack<T>.

T Push(T ob)
This method inserts an object at the top of the Stack<T>.

T Peek()
This method returns the object at the top of the Stack<T> without removing it.

bool Contains(T object)
This method checks if Stack<T> contains a specific object.

void Clear()
This method removes all of the elements from the Stack<T>.

Stack<T>.Enumerator GetEnumerator()
This method returns an IEnumerator to enumerate the entire Stack<T>.

T[ ] ToArray()
This method returns an array of type T, containing all the elements of a Stack<T>.




Advertisement




An example of Stack<T>


In this program we have created a Stack<T> to hold int values, and because C# has a unified type system, so any primitive values like int, char, float, etc are implicitly boxed into an Object reference.

Next, we will add and remove objects from it using methods. Let's see the code now -:

//C# Example of a Stack<T>


using System;
using System.Collections.Generic;

class StackEx
{
public static void Main(String[] ar)
{
	//Creating a Stack<T> of int type i.e Stack<int>, to hold int values
	Stack<int> st= new Stack<int>();
	

	//Calling the Push() method to push elements into the Stack<int>
	//New element is always added to the top of the Stack<int>
	st.Push(10);
	st.Push(32);
	st.Push(12);
	st.Push(3);
	st.Push(27);

	//Printing the contents of Stack<int>
	Console.WriteLine("The contents of Stack<int>: ");
	foreach(int element in st)
		Console.WriteLine(element);


	//Calling the Pop() method to remove the top element of the Stack<int>
	Console.WriteLine("Popping out the top element = "+ st.Pop());
	Console.WriteLine("Popping out the next top element = "+ st.Pop());


	//Calling the Peek() method which only gets the top element of the Stack<int>, without removing it
	Console.WriteLine("Peeking at the current top element = "+ st.Peek());


	//Printing the updated contents of Stack<int>
	Console.WriteLine("The updated contents of Stack<int>: ");
	foreach(int element in st)
		Console.WriteLine(element);
}
}


Output is :


The contents of Stack:
27
3
12
32
10
Popping out the top element = 27
Popping out the next top element = 3
Peeking at the current top element = 12
The pdated contents of Stack:
12
32
10


Program Analysis






Converting a Stack<T> to an array of type T


In the upcoming program, we will call the ToArray(), which returns an Object array containing all the elements of the invoked Stack<T>.

//C# Example of calling the ToArray() method of Stack<T>

using System;
using System.Collections.Generic;

class StackEx
{
public static void Main(String[] ar)
{
	//Creating a Stack<T> of char type, i.e Stack<char>, to hold characters
	Stack<char> st= new Stack<char>();
	

	//Calling the Push() method to push elements into the Stack<T>
	//New element is always added to the top of the Stack<T>
	st.Push('e');
	st.Push('a');
	st.Push('x');
	st.Push('E');
	st.Push('B');

	//Printing the contents of Stack<char>
	Console.WriteLine("The contents of Stack<char>: ");
	foreach(char element in st)
		Console.WriteLine(element);

	//Calling the ToArray() method of Stack<char>
	char[] chArr = st.ToArray();


	//Printing the contents of Object array
	Console.WriteLine("The contents of char array initialized from the elements of Stack<char>: ");
	foreach(char element in chArr)
		Console.WriteLine(element);
}
}


Output is :


The contents of Stack<char>:
B
E
x
a
e
The contents of char array initialized from the elements of Stack<char>:
B
E
x
a
e




Program Analysis






Please share this article -




< Prev
Next >
< C# Generic SortedList
C# Generic Queue >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement