Working with Generic Collections in .NET FrameworkThe increase in the usage of computers has resulted in large size of data that is used. It becomes too cumbersome to manage this data if not stored in an orderly way. The reason is that the data is not only stored but also retrieved either fully or on search basis, iterated individually and converted to a variety of possible forms. The .Net framework provides a facility to store such data in the form of Collections and allows easy access to it.
Collections are classes used as tools to store and manage a group of related objects. They also help to lookup and iterate over collection of objects. They provide options to search for item in the collection based on some search criteria. Some of the important collection classes are ArraryList, Iterator, Hashtable, Queue, SortedList, BitArray, Stack, etc. The main disadvantage of this Collection is that the datatype of the item of the collection is fixed. In case of handling value types, it causes an overhead while accessing the element due to boxing. There can also be occurrences of runtime error while trying to convert values into wrong types. Generic collection is an extension of Collection class but strongly typed to a specific datatype since similar kinds of operations (as in Collection) are performed irrespective of the type of the data being stored. Since the datatype is also passed as parameter for declaring the Collection variable, the items in the Collection object are operated with the right kind of operation. For example, the generic List class is represented as List<T> where T is the placeholder for the datatype and can implement the suitable logic (arithmetic, relational, etc.) internally with the variables of the datatype passed as parameter to it during the creation of the List object. The .Net framework itself has used generic collections within it and has implemented the classes for generic collections of different types. The generic collection classes are included in the System.Collections.Generic and System.Collections.ObjectModel namespace. Some of the generic collection classes are detailed below: Generic List class This class is used for maintaining type-safe ordered lists of objects. It is similar to ArrayList class but is strongly typed. Generic list class allows adding items, access items through the indexer syntax and iterating its collection items using foreach statement. Generic Queue class This class is used to represent a type-safe collection of items arranged in a sequential order. It is a First-In, First-Out (FIFO) collection. It supports two methods, Enqueue and Dequeue methods for adding and removing items from the collection. While adding item, its type should match the type specified in the generic type parameter of the Queue. The Peek method is used for viewing the next item without removing it. Generic Stack class This class is also used to represent a type-safe collection of items arranged in a sequential order. It is a Last-In, First-Out (LIFO) collection. It supports two methods, Push and Pop methods for adding and removing items from the collection. While adding item, its type should match the type specified in the generic type parameter of the Stack. The Peek method is used for viewing the next item without removing it. Generic LinkedList class This class
is used for storing items (in nodes) that know about their own relationship
in the list and allows iterating without accessing the collection. Each
item in the collection is an object of type, LinkedListNode. This class
is used when the objects stored in the collection need to know about their
siblings. There are many methods to add, remove and search nodes based
on the values contained in it. Unlike the Dictionary collection, while
enumerating the LinkedList collection, the value underlying the node is
directly retrieved from the list without using the LinkedListNode objects.
There is no implementation for a non-generic version of this class.
|