ASP.NET
  Home arrow ASP.NET arrow Page 3 - 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 
Moblin 
JMSL Numerical Library 
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 - Queues


    (Page 3 of 6 )


    Queues

    Lists are great when you need functionality similar to arrays. However, collections aren't just substitutes for simple arrays. Another collection is called a queue. As its name suggests, it's similar to a line of people. The first person to get in line is the first person to get out of line, the second person to get in line is the second to get out of line, and so on. A queue is a first-in-first-out (FIFO) collection. We can create a (generic) queue like this:


    Queue<int> genericQueue1 = new Queue<int>();


    Accessing the elements of a queue is unlike accessing the elements of an array or list. Instead of adding elements and accessing them with their zero-based indexes, we enqueue and dequeue them. Let's enqueue two integers with the Enqueue method:


    genericQueue1.Enqueue(11);

    genericQueue1.Enqueue(12);


    The Enqueue method adds the given object to the end of the queue. So, above, 11 is first in line, and 12 is second in line. We can access the first element in line with the Dequeue method. It removes and returns the first element in line. Here, we remove the first element:


    int someInteger = genericQueue1.Dequeue();


    Note that attempting to dequeue an element from an empty queue will raise an exception.

    As was mentioned, Dequeue removes the first element. Sometimes, it may be necessary to examine the first element without removing it from the queue. This is made possible by the Peek method, which returns the first element in the queue without removing it:


    int anotherInteger = genericQueue1.Peek();


    We can, however, loop through the contents of a Queue without disturbing them:


    foreach (int element in genericQueue1)

    {

    Console.WriteLine(element);

    }


    If we need to remove all of the elements from a Stack, the Clear method can be called:


    genericQueue1.Clear();


    Like List, Queue features a Contains method, which determines if the Queue contains a given element. Here, we search genericQueue1 for the number twelve:


    bool twelveFound = genericQueue1.Contains(12);


    It's also possible to convert a Queue into an array, just as with List:


    int[] arrayFromQueue = genericQueue1.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

    - 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
    - Developing a Dice Game Using ASP.NET Futures...
    - Completing an ASP.NET AJAX Server-Centric Ba...
    - Information Management for an ASP.NET AJAX S...
    - Comment and Order Management for an ASP.NET ...





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