Tuesday, May 28, 2013

C# Collections

Collection classes used to retrieve and store data
Most of the collection classes’ implement the specific interfaces so those classes can used with special data storage needs.
In .net framework Collection classes are defined in System.Collection namespace and generic collection classes are defined in System.Collection.Generic namespace.
Array List
Array list is ordered collection of objects and can index the individual elements. Mostly same as to the arrays but can be delete and add item at the specified position by using index. Unlike arrays, array list can resize after initialize.
Array List
Array
Can be resize after initialization
Size is fixed after the declaration
(you can copy the content of the array to another and discard the previous one if you need to change the size of current array)
Not the strongly type collection(can hold different type items)
Strongly type(can hold items with same type)
Define in System.Collection namespace
Define in System namespace
Exactly one dimension
Can have multiple dimensions
Performance wise not better than array (resizing will use CPU time if reallocation is required otherwise it will use closed memory slots during the resizing. Anyway array list should have more slightly more memory than an array)
Performance wise better than array list

Array list class implements the following interfaces,
IList à can be resize after initialization
IEnumerable à can access to elements using foreach loop
IClonable à can make deep/shallow copies
Ex:-  ArrayList arraListEx = new ArrayList();

Hash Table
Hash table is Key value collection where each item has a key value pair so key is use to access to the relative item.
Key objects must be immutable as long as they are used as keys in the Hash table.
Capacity of the hash table can automatically increase when number of elements increase.
Not a strongly type collection so we can add multiple types to the hash table but when retrieving need to use casting.

IEnumerable  à Can use foreach using DictionaryEntry type
IDictionary à can access to the element by key
Ex: - Hashtable hashtable = new Hashtable();

Hash Table
Dictionary
Not a type safe or strongly type  collection
Is a generic collection so provide type safety
When try to access to the non-existing key, null will return
When try to access to the non-existing key, ‘KeyNotFoundException’ will  return
(Refer Code 1.1)
Bit slower because of boxing/un-boxing operations
Faster for value types

Code 1.1
                class Class1
       {
        public static void Main()
        {
            Hashtable hashtable = new Hashtable();
            hashtable.Add('a', 'A');
            hashtable.Add('b', 'B');
            hashtable.Add('c', 'C');
            hashtable.Add('d', 'D');

            Dictionary<char, char> dictionary = new Dictionary<char, char>();
            dictionary.Add('a', 'A');
            dictionary.Add('b', 'B');
            dictionary.Add('c', 'C');
            dictionary.Add('d', 'D');

            Console.WriteLine(hashtable['e']);//null
            Console.WriteLine(dictionary['e']);//exception
        }
      }

Sorted List
Sorted list is a generic Key value pair collection but data of the collection is sorted by the key.
Can be access by key or index so If you access using key it behave like Hash Table or if you access using index it behave like Array. Key objects must be immutable as long as they are used as keys in the SortedList.
Capacity of the sorted list can be increase/decrease automatically when inserting/deleting elements.
IEnumerable  à Can use foreach using KeyValuePair type
   Ex:-  SortedList<int, string> sortedList = new SortedList<int, string>();
            sortedList.Add(1, "abc");
            foreach (KeyValuePair<int,string> item in sortedList)
            {
                Console.WriteLine(item.Key+"  "+item.Value);
            }
 
When try to access to the non-existing key, ‘KeyNotFoundException’ will return.


                

No comments:

Post a Comment