Working with System.Collections.Stack in C#

In the last post, Working with System.Collections.Queue, we have seen how to work with Queue using C#. In this article I will explore Stack class and the methods provided.

Stack, is a simple class which represents last-in, first-out collection (L-I-F-O) of objects and behaves exactly like, any stack in normal life should behave.

Here is class signature as at MSDN.

[SerializableAttribute]
[ComVisibleAttribute(true)]
public class Stack : ICollection, IEnumerable, 
    ICloneable

Here is how to declare and initialize Stack

           // Dclare Stack
            Stack stack;

            // initiatlize with a collection
            stack = new Stack(new int[3] { 1, 2, 3 });

            // initiatlize with initial capacity 
            stack = new Stack(10);

            // default initialization
            stack = new Stack();

Here is how we can add and remove items from stack. Remember, we can add items at the end and the very last item could be returned or removed. This means that we cannot remove other than the last entry. We can thogh, remove all items at once, we will see how.

           // insert into stack
            stack.Push(6);
            
            // remove from stack
            stack.Pop();

Here are the remaining methods and a bit of details for how to use them.

           // Return an Object at the end (Last input), without removing
            object value = stack.Peek();

            // filter and get item as per type from Stack
            IEnumerable queueEnummerable = stack.OfType<string>();

            // compare with another object
            bool IsEqualTo = stack.Equals(stack);

            // get an instance of type IQueryable
            IQueryable querable = stack.AsQueryable();

            // use Cast<T> to convert to another Collection
            List<string> list = stack.Cast<string>().ToList();

            // clear a Stack, remove all items
            stack.Clear();

            // check if Stack contains an object
            stack.Contains("one");

            // copy the contents into an Array
            string[] tempArray = new string[3];
            stack.CopyTo(tempArray, 0);

            // get total number of items in Stack
            int numberOfItems = stack.Count;

            // Get and use IEnumerator from Stack
            IEnumerator e = stack.GetEnumerator();
            while (e.MoveNext())
            {
                Object obj = e.Current;
            }

            // get type
            stack.GetType();

            // convert to an Array of Type T
            stack.ToArray();

            // get a string representation
            stack.ToString();

Working with System.Collections.Queue in C#

In the last post, Working with System.Collections.SortedList, we have seen how to work with SortedList using C#. In this article I will explore Queue class and the methods provided.

Queue, is a simple class which represents first-in, first-out collection of objects and behaves exactly like, any queue in normal life should behave.

Here is class signature as at MSDN.

[SerializableAttribute]
[ComVisibleAttribute(true)]
public class Queue : ICollection, IEnumerable, 
    ICloneable

Here is how we can declare and initialize a Queue object

            // declare Queue
            Queue queue;

            // initiatlize with initial capacity 
            queue = new Queue(10);

            // initiatlize with initial capacity 
            // and grow factor
            queue = new Queue(10, 5);

            // initiatlize with a collection
            queue = new Queue(new String[5]);

            // default initialization
            queue = new Queue();

This is how to add am object to a Queue. An object could be added to the end only

 // Add an object to the tail
queue.Enqueue("");

This is how to remove an object from a Queue. An object could be removed from front only

 // remove an object from front
 queue.Dequeue();

Here are remaining methods provided with a bit of details for what they can do for us

            // Return an Object at the front, // without removing
            object value = queue.Peek();

            // filter and get item as per type from Queue
            IEnumerable queueEnummerable = queue.OfType<string>();

            // compare with another object
            bool IsEqualTo = queue.Equals(queue);

            // get an instance of type IQquerable
            IQueryable querable = queue.AsQueryable();

            // use Cast<T> to convert to another Collection
            List<string> list = queue.Cast<string>().ToList();

            // clear an Queue
            queue.Clear();

            // Check if Queue contains an object
            queue.Contains("one");

            // copy the contents into an Array
            string[] tempArray = new string[3];
            queue.CopyTo(tempArray, 0);

            // get total number of items in Queue
            int numberOfItems = queue.Count;

            // Get and use IEnumerator from Queue
            IEnumerator e = queue.GetEnumerator();
            while (e.MoveNext())
            {
                Object obj = e.Current;
            }

            // get type
            queue.GetType();

            // convert to an Array of Type T
            queue.ToArray();

            // get a string representation
            queue.ToString();