Monday, October 18, 2010

Collections: Part 2

/*Note:
* For small Collection we generaly use ListDictionary
* For Large Collections we use HashTable
* The Best Part is to use HybridCollections it is the remedy of the above issues
*/

Hashtable hTable = new Hashtable();
hTable.Add("a",1);
hTable.Add("b", 2);
hTable.Add("c", 3);
/*
* In the above there will be no Ordering of Sequence.
* foreach (object hs in hTable.Values )
{
Console.WriteLine("Key Values are : {0}",hs.ToString());
* o/p=1,3,2
}
*/
SortedList slist = new SortedList();
slist.Add("a", 1);
slist.Add("c", 2);
slist.Add("b", 3);
/*
* Bydefault ordering is ascending
* you Can Sort by Key and Value both.
* foreach (object sls in slist.Values )
{
Console.WriteLine("Key Values are : {0}",sls.ToString());
* o/p=1,2,3
}
*/
/*==============================================================
*Note:CommonUtilClass and StringComparer Class
*==============================================================
*/
/*
* Hashtable and Sorted List are CaseInsensitive
* To make it Case Sensitive Use CollectionsUtil CollectionsUtil.CreateCaseInsensitiveHashtable();
* Use NameSpace :using System.Collections.Specialized;
* Note : In The below Case It will through an error.
* Since it is having Same Ket a & A.
*/

Hashtable hTable = CollectionsUtil.CreateCaseInsensitiveHashtable();

hTable.Add("a", 1);
hTable.Add("A", 2);
hTable.Add("c", 3);

/*
* For Making HashTable Invariant Culture u Should use below code.
*/
Hashtable hash = new Hashtable(StringComparer.InvariantCulture);
SortedList list = new SortedList(StringComparer.InvariantCulture);


Queue q = new Queue();
q.Enqueue("First");
q.Enqueue("Last");
//Console.WriteLine(q.Dequeue());
//O/p : First

Stack st = new Stack();
st.Push("1");
st.Push("6");
st.Push("2");
//Console.WriteLine(st.Peek());
//Console.WriteLine(st.Pop());
//O/p: 2
//O/p: 2

/*
* With NameValue Collection we can use same key Name for using multiple values.
*/
NameValueCollection nvc = new NameValueCollection();
nvc.Add("Same_key","Value_1");
nvc.Add("Same_key", "Value_2");

foreach (object obj in nvc.GetValues("Same_key"))
{
Console.WriteLine(obj);
//o/p: Value_1,Value_2
}
Console.Read();

No comments: