C#
  Home arrow C# arrow Page 2 - Working with C# 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  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
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? 
C#

Working with C# Collections
By: Barzan "Tony" Antal
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 13
    2008-12-16

    Table of Contents:
  • Working with C# Collections
  • Various Collections
  • Tell Me More!
  • Final Thoughts

  • 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


    Working with C# Collections - Various Collections


    (Page 2 of 4 )

    To begin with, under the term "collection" in computer science, we understand the grouping of data types that share some familiarity or significance, and with which we are able to work using the same techniques and strategies to manipulate the data stored within. Collections, unlike arrays for example, do not store a fixed amount of items.

    Now it’s time to introduce and explain a few new programming terms. The main problem is that they sound similar but the differences are huge. First off, we call sets those kinds of data array types where the order is important. Sequential access is important because that’s how we move through the items. Sorting operations on lists are frequent.

    Other kinds of data array collections generally sport index-based accessing. Random access is the main advantage compared to lists. In this section we can include collection classes such as the Hashtable, SortedList, ListDictionary, and generic classes. Fast searches and information retrieval are given by ListDictionary along with Hashtable.

    The first collection class that we are going to present here is the ArrayList. It’s based on IList and offers a lot of benefits compared to traditional old-school arrays, mostly because you can add new items anywhere (inside the list, at the end of the list, etc.), delete items, sort the list, check whether one item exists, and so forth. Most importantly, its size is dynamically increased, so you don’t have to worry about that.

    Declaring a new pointer can be done using the ArrayList() constructor; of course, the type of the data is also ArrayList. Once we’re done with this, we are able to add new items using the Add() method. We can sort the list with the Sort() function but also reverse using Reverse(). The count of items is returned using Count() property. Retrieving items can be done using Item[] and specifying the index within the brackets.

    The example below will surely make sense and illustrate the above explanation.

    using System;

    using System.Collections;

    public class SampleProgram{

    public static void Main(){

    ArrayList testArray = new ArrayList();

    testArray.Add(3);

    testArray.Add(7);

    testArray.Add(2);

    testArray.Add(5);

    foreach (int n in testArray)

    Console.WriteLine(n.ToString());

    testArray.Sort();

    testArray.Remove(5);

    testArray.Add("ASP Free");

    for (int i = 0; i < testArray.Count; i++)

    Console.WriteLine(testArray[i]);

    Console.WriteLine("Count: {0}", testArray.Count);

    Console.WriteLine("Capacity: {0}", testArray.Capacity);

    }

    }

    The output of the above program is the following. Please keep in mind that we are handling the writing out on the console in two different ways. The first one handles only ints because we declare a new int n which gets looped through the entire collection, while in the latter case we are using the int i as an index to refer to the collection’s items.

    3

    7

    2

    5

    2

    3

    7

    ASP Free

    Count: 4

    Capacity: 4

    Another collection class that we are going to present here is the Stack. This sort of class gives us “Last in, First Out” (abbreviated as LIFO) dominant behavior. This means that this order, according to which we can access and work with the elements, is given. In short, we can put items only on the top of the stack; likewise we can remove only those that are located there as well.

    The main difference compared to the built-in methods of ArrayList here is that the two major methods of the Stack class are called Push() and Pop(). These terms are quite common in computer science, and you shouldn’t be surprised if you know that these are taken for granted and “universal” in other languages too (e.g.; C++ STL.). A new item can be placed on the stack’s top with Push, while Pop removes the top item.

    It should be noted that you do not need to manually increase the index counter since this is automatically managed by the compiler. After each pop or push task the compiler decreases or increases that Count property, respectively. Moreover, the Stack has a constructor to which you can pass over, as an argument, other collection class variables. For example, you can create a stack out of an ArrayList.

    On the next page you will see another code snippet exemplifying the above explanation.

    More C# Articles
    More By Barzan "Tony" Antal


       · Thank you for reading this article. In this publication I've shown you how to work...
     

    C# ARTICLES

    - Coding a CRC-Generating Algorithm in C
    - Cyclic Redundancy Check
    - Handling Methods and Functions
    - Destroying Objects in C#
    - Creating Objects in C-Sharp
    - Classes and Objects
    - Programming Languages: Managed versus Native
    - LINQ-to-MySQL with DbLinq in C#
    - Working with Dates and Times in C#
    - Generics, Dictionaries, and More
    - More About Generics
    - Working with C# Collections
    - Generics
    - C# and XML
    - Pointers and Arrays in C#





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek