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();
Advertisements

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 )

Google+ photo

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

Connecting to %s