ASP.NET
  Home arrow ASP.NET arrow 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 
Dedicated Servers 
Actuate Whitepapers 
Moblin 
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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Basic Collections


    (Page 1 of 6 )

    Programming involves manipulating objects. However, often objects are not isolated. Rather, objects often only make sense in context with other, similar objects. Because of this, it becomes necessary to organize objects. There are two methods of organization: arrays and collections. This article will focus on collections.

    Arrays are fine if you know exactly what will be in them at compile-time or if you can determine how many elements they will contain at run-time. If you are unable to make either determination, though, then using arrays becomes problematic. Instead, when it's necessary to organize arbitrary amounts of data at run-time, collections are more suited to the task. The .NET framework contains a good many collections designed for different scenarios. This article will explore these collections.

    Lists (And a Discussion of Generic Collections)

    First, let's take a look at an array, examining how it functions. There are two ways we can define an array. The first is by providing initial values, and the second is by providing a size. Here, we define two arrays using both methods:


    string[] array1 = {"one", "two", "three"};

    int[] array2 = new int[3];


    We can access individual elements of either array by providing an index. Here, we write the first element of the first array to the console:


    Console.WriteLine(array1[0]);


    Assignment is done in a similar manner. Here, we re-assign the value of the second element of the first array, and we re-assign the values of each element of the second array:


    array1[1] = "one and a half";

    array2[0] = 1;

    array2[1] = 2;

    array2[2] = 3;


    Looping through arrays is very easy; so is using the foreach loop. Here, we write each element of the second array to the console:


    foreach (int element in array2)

    {

    Console.WriteLine(element);

    }


    From all of this, we can safely conclude that arrays are easy enough to use and manipulate. Everything about arrays is fairly straightforward. However, the problem is that arrays have fixed sizes. Both arrays in our example contain three elements – no more and no less because there's no way to change this. For example, suppose I wanted to gather all of the prime numbers less than one hundred thousand. If I made each prime number an element in an array, I would have to know beforehand how many prime numbers are under one hundred thousand. That would be quite inconvenient.

    This is where collections come in. Collections contain data just as arrays do, but they offer more functionality (and different functionality) and flexibility. Suppose there was a way to create something similar to an array in behavior except that it would feature a dynamic size. This is exactly what a list does! It works similarly to an array, but you can add and remove elements as needed. A list called ArrayList is located in the System.Collections.List namespace. It will store any number of objects. Here, we define an ArrayList:


    ArrayList arrayList1 = new ArrayList();


    Notice how we don't specify the size as we do with a regular array. This isn't necessary. In order to add elements into the ArrayList, the Add method is used, which accepts an object. Let's add three elements to our ArrayList:


    arrayList1.Add("five");

    arrayList1.Add("six");

    arrayList1.Add("seven");


    Additionally, with .NET 3.0, the initial elements of an ArrayList can be added on the same line as the initialization of the ArrayList itself, as with arrays:


    ArrayList arrayList1 = new ArrayList() { "five", "six", "seven" };


    Elements in an ArrayList can be accessed by their zero-based indexes, just as in arrays. However, recall that an ArrayList's elements are all of type object. This means that in order to use a given element, it must be cast into its proper type. Here, we extract the first element from arrayList1:


    string someString = (string)arrayList1[0];


    This, however, looks messy. Moreover, it's also error-prone. For example, an int could be added to our ArrayList (it will be implicitly cast to object):


    arrayList1.Add(8);

    More ASP.NET Articles
    More By Peyton McCullough


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

    ASP.NET ARTICLES

    - 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 ...
    - Back-end Management Tasks for an ASP.NET AJA...
    - User Information Management for an ASP.NET A...
    - Adding Comments and Search to an ASP.NET AJA...
    - Order-Related Modules for an ASP.NET AJAX Se...
    - User and Role Management for an ASP.NET AJAX...
    - Programming an ASP.NET AJAX Server-Centric B...





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