Working with Generic Collections in .NET Framework

The 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.

Generic Dictionary

Since a Dictionary uses both a key and a value for each item in its collection, two generic type parameters have to be specified while creating an instance of the Dictionary object. Unlike the non-generic Dictionary class, generic Dictionary class does not use DictionaryEntry object to hold the key/value pair. Instead, a new generic type, KeyValuePair class is used.
The members, Key and Value are used to store the key/value data of each of the item in the generic dictionary collection. The indexer syntax is used to add or retrieve the items in the Dictionary. Care should be taken to use the right type of data to key and value part of the item such that it matches the one specified as generic parameter during the creation of the Dictionary object.

Generic SortedList and SortedDictionary class

These classes are similar to generic List and Dictionary classes except that the items in the collection are sorted by the key of the collection.

Generic interfaces implementing generic collections

The ICollection interface is the base interface for generic collection types which implements the common functionality of addition, removal, copy and enumeration of the elements of the collection. This interface is inherited from IEnumerbale<T>(generic) and IEnumerable(nongeneric) interfaces. Another generic interface which implements the Dictionary class is IDictionary<TKey, TValue>. This interface implements the logic for retrieval of collection items based on key. The interface, IEnumerable<T> is used as base for the generic collections for enumerating items in the collection. The interfaces, IComparable and IEquatable are used for ordering comparisons and equality comparisons respectively.

The main advantage of using generics is to reduce code redundancy and better re-use. Also, the need for type checking and enforcing the right way of typecasting is taken care by the compiler with considerable reduction in the occurrences of runtime error. Performance of generic collection of value types is better when compared to non-generic ones since the overhead in boxing the value of Object type can be avoided.

Tips while using generic collections:

• If base class is passed as parameter to the generic class, derived class (of the base class) cannot be used as parameter during object creation.

• By passing more parameters to the generic collection classes, the code can be re-used more but it should not reduce the readability to the user.

• Constraints can be specified and used to avoid unintended usage of the parameters and hence avoid errors due to lack of checking.

|Developing .Net applications for multiple locales | List of Conversion Keywords in C# |Storage and transfer of data using Serialization in .NET |C# (C Sharp) Unified Type System: Reference Types |Using Reflection at Runtime in .NET Framework |What are Generics in C# |Working with Generic Collections in .NET Framework |Working with Graphics in .Net |Working with Isolated Storage in .NET|

“Amazon and the Amazon logo are trademarks of, Inc. or its affiliates.”

| Privacy Policy for | Disclosure | Contact |

Copyright - © 2004 - 2024 - All Rights Reserved.