There are the following 2 types of collections:
- Non-Generic
- Generic
Non-generic Generic
ArrayList -------------> List
HashTable -------------> Dictionary
SortedList -------------> SortedList
Stack -------------> Stack
Queue -------------> Queue
1. Non-Generic
1. Non-Generic
- Each element can represent a value of a different type.
- Array Size is not fixed.
- Elements can be added / removed at runtime.
ArrayList
- Arraylist is a class that is similar to an array, but it can be used to 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
ForeachLoop
It executes for each and every item that exists in the arraylist object.
Every time the loop rotates it reads one item from the arraylist and assignes it to the variable.
It executes for each and every item that exists in the arraylist object.
Every time the loop rotates it reads one item from the arraylist and assignes 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.
it reduces the memory allocated for the object.
Capacity: is a property that returns the number of items for which memory is allocated .
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: is a class whose object represents the data in a combination of key & value pairs.
SortedList:
- Is a class that has the combination of arraylist and hashtable.
- Represents the data as a key and value pair.
- Arranges all the items in sorted order.
- using System.Collections;
- protected void Button3_Click(object sender, EventArgs e)
- {
- SortedList sl = new SortedList();
- sl.Add("ora", "oracle");
- sl.Add("vb", "vb.net");
- sl.Add("cs", "cs.net");
- sl.Add("asp", "asp.net");
- foreach (DictionaryEntry d in sl)
- {
- Response.Write(d.Key + " " + d.Value);
- Response.Write("<br>");
- }
- }Output:
cs cs.net
ora oracle
vb vb.net
- protected void Button4_Click(object sender, EventArgs e)
- {
- Stack stk = new Stack();
- stk.Push("cs.net");
- stk.Push("vb.net");
- stk.Push("asp.net");
- stk.Push("sqlserver");
- foreach (object o in stk)
- {
- Response.Write(o + "<br>");
- }
- }Output:sqlserver
asp.net
vb.net
cs.net
- using System.Collections;
- protected void Button5_Click(object sender, EventArgs e)
- {
- Queue q = new Queue();
- q.Enqueue("cs.net");
- q.Enqueue("vb.net");
- q.Enqueue("asp.net");
- q.Enqueue("sqlserver");
- foreach (object o in q)
- {
- Response.Write(o + "<br>");
- }
- } Output:cs.net
vb.net
asp.net
sqlserver
2. Generic Collections
Generic Collections work on the specific type that is specified in the program whereas non-generic collections work on the object type.- Specific type
- Array Size is not fixed
- Elements can be added / removed at runtime.
List:
- using System.Collections.Generic;
- protected void Button1_Click(object sender, EventArgs e)
- {
- List<int> lst = new List<int>();
- lst.Add(100);
- lst.Add(200);
- lst.Add(300);
- lst.Add(400);
- foreach (int i in lst)
- {
- Response.Write(i+"<br>");
- }
- }
Dictonary:
SortedList:
- using System.Collections.Generic;
- protected void Button1_Click(object sender, EventArgs e)
- {
- Dictionary<int, string> dct = new Dictionary<int, string>();
- dct.Add(1, "cs.net");
- dct.Add(2, "vb.net");
- dct.Add(3, "vb.net");
- dct.Add(4, "vb.net");
- foreach (KeyValuePair<int, string> kvp in dct)
- {
- Response.Write(kvp.Key + " " + kvp.Value);
- Response.Write("<br>");
- }
- }
- using System.Collections.Generic;
- protected void Button3_Click(object sender, EventArgs e)
- {
- SortedList<string, string> sl = new SortedList<string, string>();
- sl.Add("ora", "oracle");
- sl.Add("vb", "vb.net");
- sl.Add("cs", "cs.net");
- sl.Add("asp", "asp.net");
- foreach (KeyValuePair<string, string> kvp in sl)
- {
- Response.Write(kvp.Key + " " + kvp.Value);
- Response.Write("<br>");
- }
- }
- using System.Collections.Generic;
- protected void Button4_Click(object sender, EventArgs e)
- {
- Stack<string> stk = new Stack<string>();
- stk.Push("cs.net");
- stk.Push("vb.net");
- stk.Push("asp.net");
- stk.Push("sqlserver");
- foreach (string s in stk)
- {
- Response.Write(s + "<br>");
- }
- }
- using System.Collections.Generic;
- protected void Button1_Click(object sender, EventArgs e)
- {
- Queue<string> q = new Queue<string>();
- q.Enqueue("cs.net");
- q.Enqueue("vb.net");
- q.Enqueue("asp.net");
- q.Enqueue("sqlserver");
- foreach (string s in q)
- {
- Response.Write(s + "<br>");
- }
- }
Difference between Array and ArrayList in C#
The following table lists the difference between Array and ArrayList in C#.
Array | ArrayList |
---|---|
Array is strongly typed. This means that an array can store only specific type of items\elements. | ArrayList can store any type of items\elements. |
Array stores fixed number of elements. Size of an Array must be specified at the time of initialization. | ArrayList grows automatically and you don't need to specify size. |
No need to cast elements of an array while retriving because it is strongly type and stores specific type of items only. | Items of ArrayList need to be cast to appropriate data type while retriving. |
Use static helper class Array to perform different tasks on the array. | ArrayList itself includes various utility methods for various tasks. |
No comments:
Post a Comment