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. Collection types implement the following common functionality:
- Adding and inserting items to a collection
- Removing items from a collection
- Finding, sorting, and searching items
- Replacing items
- Copy and clone collections and items
- Capacity and Count properties to find the capacity of the collection and the number of items in the collection
.NET supports two types of collections, generic collections, and non-generic collections. Before NET 2.0, it was just collections, and when generics were added to .NET, generics collections were added as well.
Learn more about generics here: Generics in C#.
The following table lists and matches these classes.
Non-generic Generic
ArrayList -------------> List
HashTable -------------> Dictionary
SortedList -------------> SortedList
Stack -------------> Stack
Queue -------------> Queue
1. Non-Generic
In non-generic collections, each element can represent a value of a different type. The collection size is not fixed. Items from the collection can be added or removed at runtime.
C# ArrayList
ArrayList class is a collection used for any types or objects.
- Arraylist is a class similar to an array, but it can store values of various types.
- An Arraylist doesn't have a specific size.
- Any number of elements can be stored.
using System.Collections;
protected void Button1_Click(object sender, EventArgs e)
{
ArrayList al = new ArrayList();
string str = "kiran teja jallepalli";
int x = 7;
DateTime d = DateTime.Parse("8-oct-1985");
al.Add(str);
al.Add(x);
al.Add(d);
foreach (object o in al)
{
Response.Write(o);
Response.Write("<br>");
}
}
Output
kiran teja jallepalli
7
10/8/1985 12:00:00 AM
Foreach Loop
It executes every item that exists in the arraylist object. Every time the loop rotates, it reads one item from the arraylist and assigns it to the variable.
Note: Arraylist allocates memory for 4 items whenever an object is created. When a fifth item is added, memory for another 4 items are added. It reduces the memory allocated for the object.
Capacity: is a property that returns the number of items for which memory is allocated.
Here is a detailed tutorial: ArrayList in C#.
C# HashTable
HashTable is similar to arraylist but represents the items as a combination of a key and value.
using System.Collections;
protected void Button2_Click(object sender, EventArgs e)
{
Hashtable ht = new Hashtable();
ht.Add("ora", "oracle");
ht.Add("vb", "vb.net");
ht.Add("cs", "cs.net");
ht.Add("asp", "asp.net");
foreach (DictionaryEntry d in ht)
{
Response.Write (d.Key + " " + d.Value);
Response.Write("<br>");
}
}
Output
vb vb.net
asp asp.net
cs cs.net
ora oracle
DictonaryEntry: a class whose object represents the data in a combination of key & value pairs.
Learn more here: HashTable in C#

Join the conversation! Your thoughts help the community grow.