Advertisement
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 | 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 |
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
//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);
}
}
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
//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);
}
}
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
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement