ASP.NET
  Home arrow ASP.NET arrow Page 4 - Basic Collections
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP.NET

Basic Collections
By: Peyton McCullough
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 4
    2007-11-13

    Table of Contents:
  • Basic Collections
  • Lists Continued
  • Queues
  • Stacks
  • Dictionaries
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Basic Collections - Stacks


    (Page 4 of 6 )

    Stacks

    Stacks are similar to queues in that you can't directly access most of the elements. However, a stack is a last-in-first-out (LIFO) collection. Imagine, for example, a stack of papers. Let's be more specific and say that it's a short stack of two papers. While the bottom paper was the first paper to be put into the stack, it cannot be accessed without first taking off the top piece of paper. This is how a stack, which is represented by the appropriately-named Stack class, operates. The last element to be put into, or pushed onto, a Stack is the first element that may be taken out, or popped from the Stack. Let's create a (generic) Stack of string objects:


    Stack<string> genericStack1 = new Stack<string>();


    Elements can be pushed onto the stack using the Push method. Here, we push three strings onto the stack:


    genericStack1.Push("first");

    genericStack1.Push("second");

    genericStack1.Push("third");


    The last element can be popped off (which will remove the item) using the Pop method:


    string lastOn = genericStack1.Pop();


    As with a Queue, we can also peek at the last element without removing it by using the Peek method:


    string notRemoved = genericStack1.Peek();


    We can also loop through the elements of a Stack without disturbing them:


    foreach (string element in genericStack1)

    {

    Console.WriteLine(element);

    }


    Stack also features a Contains method, a Clear method and a ToArray method which all work just as they do in a Queue:


    bool fourthFound = genericStack1.Contains("fourth");

    genericStack1.Clear();

    string[] willNotContainAnything = genericStack1.ToArray();

    More ASP.NET Articles
    More By Peyton McCullough


       · Hello,This is an introduction to basic collections, with an emphasis on generic...
     

    ASP.NET ARTICLES

    - Disadvantages of the ASP.NET MVC Framework
    - Advantages of the ASP.NET MVC Approach
    - ASP.NET Web Forms Weaknesses
    - ASP.NET Web Forms Meets ASP.NET MVC
    - Source Code for Saving and Retrieving Data w...
    - Using GridView to Save and Retrieve Data wit...
    - Handling Dynamic Images in ASP.NET 3.5 AJAX ...
    - Retrieving Data with AJAX and the GridView C...
    - Playing with Images in ASP.NET 3.5 AJAX Appl...
    - Saving and Retrieving Data with AJAX
    - Enhancing PHP Via the ASP.NET AJAX Framework...
    - Enhancing PHP Programming with the ASP.NET A...
    - Classes and ASP.NET AJAX
    - Using ASP.NET AJAX
    - Building a Simple Storefront with LINQ





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
    Stay green...Green IT