CSharp.Net-Informations.com
   Home      .Net Framework      VB.NET      C#                                                                      About


  How to use C# Stack Class

The Stack class represents a last-in-first-out (LIFO) Stack of Objects. Stack follows the push-pop operations. That is we can Push (insert) Items into Stack and Pop (retrieve) it back . Stack is implemented as a circular buffer. It follows the Last In First Out (LIFO) system. That is we can push the items into a stack and get it in reverse order. Stack returns the last item first. As elements are added to a Stack, the capacity is automatically increased as required through reallocation.

Commonly used methods :

  Push	: Add (Push) an item in the Stack data structure
  Pop	: Pop return the last Item from the Stack

Contains: Check the object contains in the Stack

Push : Add (Push) an item in the Stack data structure

  Syntax : Stack.Push(Object)
  Object : The item to be inserted.
  Stack days = new Stack();

days.Push("Sunday");

Pop : Pop return the item last Item from the Stack

  Syntax : Object Stack.Pop()
  Object : Return the last object in the Stack

days.Pop();

Contains : Check the object contains in the Stack

  Syntax : Stack.Contains(Object)
  Object : The specified Object to be search

days.Contains("Tuesday");

The following CSharp Source code shows some of important functions in Stack Class:

         C# Source Code Download           Print Source Code
         How to use C# Stack Class - Download
        
C# Tutorial

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Stack days = new Stack();
            days.Push("SunDay");
            days.Push("MonDay");
            days.Push("TueDay");
            days.Push("WedDay");
            days.Push("ThuDay");
            days.Push("FriDay");
            days.Push("SaturDay");
            if (days.Count ==7)
            {
                MessageBox.Show(days.Pop().ToString ());
            }
            else
            {
                MessageBox.Show("SaturDay does not exist");
            }
        }
    }
}

When you execute this C# program add seven items in the stack . Then it check the count is equal to 7 , if it is seven then pop() the item. The message box will display SaturDay.

CSharp Collection Related Contents
*     How to use C# ArrayList Class
*     How to use C# HashTable Class
*     How to use C# Queue Class
*     How to use C# NameValueCollection Class
*     How to use Array in C#
*     How to for each loop in C# Array


   Home      VB.NET      C#
CSharp Related Topics
*     An overview of Microsoft CSharp
*     C# Language Tutorial
*     C# Statements Tutorial
*     C# Collection Tutorial
*     C# String Tutorial
*     C# File Operations Tutorial
*     C# Excel Tutorial
*     C# Crystal Reports Tutorial
*     CSharp Communication Tutorial
*     C# Ado.Net Tutorial and Source Code
*     C# ADO.NET data Providers Tutorial
*     C# Dataset Tutorial
*     C# DataAdapater Tutorial
*     Csharp DataView Tutorial
*     Csharp Remoting Tutorial
*     C# XML Tutorial
*     C# DataGridView Tutorial
   Home      VB.NET      C#
More Source Code :   
Mail to :  feedback@net-informations.com
  |  Home   |  VB.NET   |  C#   |  SiteMap   |  Terms of Use   |  About   |
net-informations.com (C) 2010
All Rights Reserved. All other trademarks are property of their respective owners.