Wednesday, 19 September 2012

Collecting Data Items

Computers are usually good at dealing with large amounts of data. In our daily work as developer, we will find it necessary to store data in an orderly way. The .NET framework supports dealing with data in the way by providing a wide range of collections to store our data in. For every collection job, the .NET Framework supplies a solution. 

Types of Collections

The .NET Framework's System.Collections namespace supports several types of collections. These collections are classes that support the gathering of information in an orderly way. Various types of collections are:

        Name                                                         Description 

  • ArrayList                                                  A simple resizable, index-based collection of bytes
  • SortedList                                               A sorted collection of name/value pairs of objects.
  • Queue                                                     A first-in, first-out collection of objects.
  • Stack                                                      A last-in, first-out collection of objects.
  • BitArray                                                  A compact collection of Boolean values.
  • StringCollection                                    A simple resizeable collection of strings. 
  • LastDictionary                                      An efficient collection to store small lists of objects.

Adding and Removing Items

The ArrayList class is a simple, unordered container for objects of any type. Adding items to and removing items from the class is very straightforward. The Add method allows us to add a single object to the collection. We can use the Add method to store any object in .NET. For example:
In C#
coll.Add("hi");
In contrast, the RemoveAt method removes an item at a particular index within the collection. For example:
In C#
coll.RemoveAt(0);

Iterating Over Items

A collection is not very useful unless we can walk through the items in it. The ArrayList supports several ways to iterate over its contents. The ArrayList supports a numeric indexer that allows us to writesimple code to show the items in order. For example: 
In C#
for(int x=0; x<coll.count; ++x)
{
Console.WriteLine(coll[x]);
}

No comments:

Post a Comment