What are Collections Collections are the group of similar and dissimilar objects.Thorugh collections we can implement different various structures. Collections implement various classes as well Collections provide a way to store arbitrary objects in a structured fashion, and we all know how useful they are in everyday programming.
Need of Collections 1.Gives Non Persistent Data Structure. 2.It provides specialized classes for data storage and retrieval. 3. The interfaces may be inherited to create new collection classes. 4. Well tested and efficient methods are provided so user does not have to spent time in constructing methods.
Non Generic Collections <ul><li>These Collections contains interfaces and classes that define various collections of objects, such as lists, queues, bit arrays, hash tables and dictionaries </li></ul><ul><li>Non Generic Collections are not type safe the data we supply is converted to object. </li></ul><ul><li>It implements one or more of the ICollection,IDictionary,IEnumerable,IComparable and IList interfaces. </li></ul>
Categories of Non Generic Collections <ul><li>Ordered collections: The Stack and Queue classes are both examples of Ordered collections implementing ICollection Interface. </li></ul><ul><li>Indexed collections: Array List class is one example of an indexed collection implementing IList Interface. </li></ul><ul><li>Keyed collections: The Hash Table class is one example of an Indexed Collection that implements the IDictionary interface. </li></ul>
Generic Collections It allow users to create collections that provide better type safety and performance than non-generic strongly typed collections .Applications that target version 2.0 and later of the .NET Framework should use the generic collection classes Example :- instead of using the Hashtable class, which allows keys and values to be of any type, you can use the Dictionary generic class and specify the type allowed for the key and the type allowed for the value.
Generic vs. Non Generic Collections Non-Generic Similar Generic Type ArrayList List<T> Hashtable Dictionary<TKey,TValue> SortedList SortedList<TKey,TValue> Queue Queue<T> Stack Stack<T> N/A SortedDictionary<TKey,TValue> N/A KeyedCollection<TKey,TItem> N/A LinkedList<T>
Conclusion Collections are a vital element of any object-oriented architecture. Offering the capability to group objects in logical constructs, they improve code readability and self documentation, as well as enhance maintainability. As we see that collections reduces the Programming effort and increases program speed and Quality so it is always a good practice to use collections.