What is Collection ? It's just are enhancement to the arrays. before collection we have array where we perform operation or arranged data in some specified way. Like i want to arrange data for employee salary. So i can create a Floating number array, but there is some limitation of array because array has fixed length and if my employee count is increasing then i have to increase length of array. Another limitation of array is that array has same data type. If you want to store employee name and salary then you can't use one array.
Collection remove this drawback.
Below are the type's of collection.
ArrayList
Dictionary
Hash Table
SortedList
Stack
Queue
NameValueCollection
--- ArrayList ----
ArrayList is a ordered collection of an object that can be indexed individually.
We can add any number of item dynamically.
Like :
ArrayList list = new ArrayList();
list.Add(1);
list.Add(2);
list.Add('p');
list.Add('Q');
list.Add('R');
list.Add('S');
<pre> list.Insert(3,"Array"); Insert any element at particular location.. here this element insert after the 'P' element.
list.Remove(9); Similarly we can remove any element by giving location index.
list.RemoveAt(2);
foreach (var i in list)
{
Console.WriteLine(i);
}
----- Dictionary---
With Dictionaries, we use keys and values of any type, including int and string. Dictionaries search the value by on keys, and we can get value by this key. Dictionary class is a strongly types < T Key, T Value > and you must specify the data types for both the key and value.
Dictionary<string, DateTime> dStud = new Dictionary<string, DateTime>();
dStud.Add("Current Date", DateTime.Now);
foreach (var a in dStud)
{
Console.WriteLine("Key : " + a.Key + " Value : " + a.Value);
}
---- Hash Table----
Hash Table is much similar with Dictionary. The difference is hash table is not a generic type. In Hashtable, we can add keys and values of any Object Type.
Like below example :-
Hashtable hTable = new Hashtable();
hTable[0] = "1 Value";
hTable[1] = "2 Value";
hTable[2] = 2;
hTable[3] = 3;
hTable[Convert.ToDateTime("02/14/2012")] = "Valentine day";
foreach (DictionaryEntry a in hTable)
{
Console.WriteLine(a.Key + " " + a.Value);
}
-------------Stack -------------
Stack is a LIFO collection. It provides a powerful and simple last-in-first-out data structure.
Stack sStack = new Stack();
sStack.Push(11);
sStack.Push(12);
sStack.Push("abc");
sStack.Push("xyz");
sStack.Pop(); // Here "xyz" id removed from stack
sStack.Push(14);
sStack.Push(15);
foreach (var i in sStack)
{
Console.WriteLine(i);
}
var peek = sStack.Peek();
Console.WriteLine("--- Element now at the top ---");
Console.WriteLine(peek);
------ Queue -------
Queue is a FIFO collection. It processes elements in a first-in, first-out order.
Queue qQueue = new Queue();
qQueue.Enqueue("a");
qQueue.Enqueue(2);
qQueue.Enqueue("b");
qQueue.Dequeue();
foreach (var value in qQueue)
{
Console.WriteLine(value);
}
---- Name Value Collection -----
---- using System.Collections.Specialized; -----
In Our Dictionary we can not use same key again. there is a restriction of key name.
NameValueCollection to overcome this problem. if we need such data where key can be multiple then we can use NameValueCollection, here we can have multiple name and if we can access
our element by that name then it will return all value corresponding to that name.
NameValueCollection markStatus = new NameValueCollection();
markStatus.Add("Very High", "80");
markStatus.Add("High", "60");
markStatus.Add("High", "50");
markStatus.Add("Pass", "40");
Console.WriteLine(markStatus["High"]);
Reference
http://www.dotnetperls.com/collections
0 Comment(s)