Collections in C#

C# collection types are designed to store, manage and manipulate similar data more efficiently. Data manipulation includes adding, removing, finding, and inserting data in the collection. Generics provide stronger type checks at compile time, elimination of cast and a boost in performance. .NET supports two types of collections, generic collections and non-generic collections. Prior to .NET 2.0, it was just collections.

Non-generic Generic API Usage
ArrayList List Add() use when you need to access each element by index
HashTable Dictionary Add(key, value) use when you need to access each element by key
SortedList SortedList Add() sorts elements as soon as added
Stack Stack Push(), Pop() use when you need LIFO access
Queue Queue Enqueue(), Dequeue() use when you need FIFO access

ArrayList :: List<T>
It represents ordered collection of an object that can be indexed individually. It is basically an alternative to an array. However, unlike array you can add and remove items from a list at a specified position using an index and the array resizes itself automatically. It also allows dynamic memory allocation, adding, searching and sorting items in the list.

Hashtable :: Dictionary<T, T>
It uses a key to access the elements in the collection. A hash table is used when you need to access elements by using key, and you can identify a useful key value. Each item in the hash table has a key/value pair. The key is used to access the items in the collection.

SortedList
It uses a key as well as an index to access the items in a list. A sorted list is a combination of an array and a hash table. It contains a list of items that can be accessed using a key or an index. If you access items using an index, it is an ArrayList, and if you access items using a key , it is a Hashtable. The collection of items is always sorted by the key value.

Stack
It represents a last-in, first out collection of object. It is used when you need a last-in, first-out access of items. When you add an item in the list, it is called pushing the item and when you remove it, it is called popping the item.

Queue
It represents a first-in, first out collection of object. It is used when you need a first-in, first-out access of items. When you add an item in the list, it is called enqueue and when you remove an item, it is called deque.

BitArray
It represents an array of the binary representation using the values 1 and 0. It is used when you need to store the bits but do not know the number of bits in advance. You can access items from the BitArray collection by using an integer index, which starts from zero.

Generic Collections
using System.Collections.Generic;

// List
List<int> lst = new List<int>();
lst.Add(100);
lst.Add(200);
lst.Add(300);

foreach (int ii in lst)
{
    Response.Write(ii + "<br>");
}

// Dictionary
Dictionary<int, string> dct = new Dictionary<int, string>();
dct.Add(1, "cs.net");
dct.Add(2, "vb.net");
dct.Add(3, "vb.net");

foreach (KeyValuePair<int, string> kvp in dct)
{
    Response.Write(kvp.Key + " " + kvp.Value + "<br>");
}

// SortedList
SortedList<string, string> sl = new SortedList<string, string>();
sl.Add("ora", "oracle");
sl.Add("vb", "vb.net");
sl.Add("cs", "cs.net");

foreach (KeyValuePair<string, string> kvp in sl)
{
    Response.Write(kvp.Key + " " + kvp.Value + "<br>");
}

// Stack - Push() Pop()
Stack<string> stk = new Stack<string>();
stk.Push("cs.net");
stk.Push("vb.net");
stk.Push("asp.net");

foreach (string str in stk)
{
    Response.Write(str + "<br>");
}

// Queue - Enqueue() Dequeue()
Queue<string> qu = new Queue<string>();
qu.Enqueue("cs.net");
qu.Enqueue("vb.net");
qu.Enqueue("asp.net");

foreach (string str in qu)
{
    Response.Write(str + "<br>");
}

Non-generic Collections
using System.Collections;

// ArrayList
ArrayList al = new ArrayList();
al.Add("virat jetty"");
al.Add(7);
al.Add(DateTime.Parse("17-Aug-2001"));

foreach (object ob in al)
{
	Response.Write(ob + "<br>");
}

// Hashtable
Hashtable ht = new Hashtable();
ht.Add("ora", "oracle");
ht.Add("vb", "vb.net");
ht.Add("cs", "cs.net");

foreach (DictionaryEntry de in ht)
{
    Response.Write (de.Key + " " + de.Value + "<br>");
}

// SortedList
SortedList sl = new SortedList();
sl.Add("ora", "oracle");
sl.Add("vb", "vb.net");
sl.Add("cs", "cs.net");

foreach (DictionaryEntry de in sl)
{
    Response.Write(de.Key + " " + de.Value + "<br>");
}

// Stack - Push() Pop()
Stack stk = new Stack();
stk.Push("cs.net");
stk.Push("vb.net");
stk.Push("asp.net");

foreach (object ob in stk)
{
    Response.Write(ob + "<br>");
}

// Queue - Enqueue() Dequeue()
Queue qu = new Queue();
qu.Enqueue("cs.net");
qu.Enqueue("vb.net");
qu.Enqueue("asp.net");

foreach (object ob in qu)
{
    Response.Write(ob + "<br>");
}



Ginger CMS
the future of cms, a simple and intuitive content management system ...

ASP.NET MVC Application
best practices like Repository, LINQ, Dapper, Domain objects ...

CFTurbine
cf prototyping engine, generates boilerplate code and views ...

Search Engine LITE
create your own custom search engine for your web site ...

JRun monitor
monitors the memory footprint of JRun engine and auto-restarts a hung engine ...

Validation Library
complete validation library for your web forms ...