Collection classes are used for data storage and manipulate (sort, insert, remove etc) the data.

Most of the collection classes implement the same interfaces, and these interfaces may be inherited to create new collection classes on the basis of more specialized data. These collection classes are defined in System.Collections.Generic.

Main collection classes which are used in c# as

  •    ArrayList Class
  •    HashTable Class
  •    Stack Class and Queue class etc.

The main properties of the collection classes are

·         Collection classes are defined as part of the System.Collection or System.Collections.Generic namespace.

·         Most collection classes derive from the interfaces ICollection, IComparer, IEnumerable, IList, IDictionary, and IDictionaryEnumerator and their generic equivalents.

·         Using generic collection classes provides increased type-safety and in some cases can provide better performance, especially when storing value types.

The following generic types correspond to existing collection types:

·         List is the generic class corresponding to ArrayList.

·         Dictionary is the generic class corresponding to Hashtable.

·         Collection is the generic class corresponding to collectionBase.Collection can be used as a base class, but unlike CollectionBase it is not abstract, making it much easier to use.

·         ReadOnlyCollection is the generic class corresponding to ReadonlyCollection. ReadOnlyCollection is not abstract, and has a constructor that makes it easy to expose an existing List as a read-only collection.

·         The Queue, stack and SortedList generic classes correspond to the respective nongeneric classes with the same names.

List Generic Class or ArrayList

The List class is the generic equivalent of the ArrayList class. It implements the IList generic interface using an array whose size is dynamically increased as required.

ArrayList is the part of show the simple list value. ArrayList class contain the Add, Insert Remove, RemoveAt and  Sort method and main properties like Capacity, Count etc. It is the part of System.Collection.

Syntax for creating a ArrayList:

ArrayList name = new ArrayList();

Syntax for creating a List<T>:

List<Type> name=new List<Type>();

There are some basic properties and methods of List<T>

1.       Adding item to list

2.       Removing item to list

3.       Sort the list

4.       Insert the item into the list etc.

Add the item in ArrayList
  //make object of ArrayList class like countryList
        ArrayList countryList = new ArrayList();
        //Add the country in the countrylist
        //Show the countryList
        Response.Write("<b><u>Country List:</u></b><br/>");
        foreach (string country in countryList)
            Response.Write(country + "<br/>");
Add the item in List<T>
  //define the List here
        List<string> countryList = new List<string>();
        //use the add method to add the element in List
        //print the data on web page
        Response.Write("<b><u>Country List:</u></b><br/>");
        foreach (string country in countryList)
           Response.Write(country +"<br/>");


Country List:

Remove the item from List<T>

List have following remove properties: Remove(), RemoveAt(), RemoveAll(), RemoveRange().



Insert item into the List<T>

Insert method is used for Insert item into the List at any index of the list.



Sort the item from the List<T>

countryList.Sort ();


Note: Other method which is used in List<T>

IndexOf(),Contains(),TrimExcess(),Clear() etc.


The SortedList object contains items in key/value pairs. SortedList objects automatically sort the items in alphabetic or numeric order. Main method of sorted list are Add(), Remove(), IndexOfKey(), IndexOfValue(), GetKeyList(), GetKeyValue() etc. and to object key and values.

       //make object of Sorted List class like countryTable
        SortedList countrySList = new SortedList();
        //Add the country in the hashtable.Add(Object key,Object value)
        countrySList.Add(1, "india");
        countrySList.Add(2, "England");
        //Find the key and value by using DictionaryEntry
        foreach (DictionaryEntry country in countrySList)
            Response.Write(country.Key + " : " + country.Value + "<br/>");


Another Example related to SortedList check this link

How use the GetKeyList() and GetKeyValue() method
IList countryKey = countrySList.GetKeyList();
foreach(Int32 country in countryKey)
    Response.Write(country +"<br/>");

Where IList is the interface of System.Collection.IList

IList countryValue = countrySList.GetValueList();
foreach (Int32 country in countryKey)
    Response.Write(country + "<br/>");


Hashtable in C# represents a collection of key/value pairs which maps keys to value. Any non-null object can be used as a key but a value can. We can retrieve items from hashTable to provide the key. Both keys and values are Objects.

The main properties of HashTable are Key and Values and methods add (), remove (), contains () etc.

How use HashTable in .net
  //make object of HashTable class like countryTable
        Hashtable countryTable = new Hashtable();
        //Add the country in the hashtable.Add(Object key,Object value)
        countryTable.Add(1, "India");
        countryTable.Add(2, "Srilanka");
        countryTable.Add(3, "England");
        //Find the key and value by using DictionaryEntry class
        foreach (DictionaryEntry country in countryTable)
            Response.Write(country.Key + " : " + country.Value +"<br/>");


Country List:
3 : England
2 : Srilanka
1 : India

Detailed concept on hash table sees this link:


Stack Class

It is worked as Last in first out (LIFO) when making the object of the stack class. Stack follow the two important method Push () and Pop ().  Push () method is used for inserting the item and pop () method is used for the removing the data.

Push () method


  //make object of Stack class 
        Stack countryStack = new Stack();
        //Insert the item by push method
        //show the element in the stack
        foreach(string country in countryStack)
            Response.Write(country + "<br/>");


<![if !supportLineBreakNewLine]>

Pop () method:

//Remove the item from the list


Queue Class

Queue work as first in first out (FIFO). Queue class has main method enqueue() and dequeue().Objects stored in a Queue are inserted at one end and removed from the other. The Queue provides additional insertion, extraction, and inspection operations. We can Enqueue (add) items in Queue and we can Dequeue (remove from Queue) or we can Peek (that is we will get the reference of first item) item from Queue. Queue accepts null reference as a valid value and allows duplicate elements.

The main method and properties of the queue class are Enqueue(), Dequeue() and Peek() etc.


  //make object of the queue class
        Queue countryQueue = new Queue();
        //insert the item in queue by Enqueue method
        //remove the item in queue by Dequeue method
        foreach (string country in countryQueue)
            Response.Write(country + "<br/>");

Its main properties are Next and Previous so it is allow the forward and reverse traversal by these properties and its main methods AddAfter(), AddFirst(), AddBefore(), AddHead(), AddLast() and AddTail.

  Modified On Nov-30-2017 06:28:26 AM

Leave Comment