Wednesday, 26 June 2013

Insight Collections

Note: All this data has been copied from various sources for the purpose of knowledge sharing. Data content  can be either as it is or with little modification.
---------------------------------------------------------------------------------------------------------------------

Collection classes serve various purposes, such as allocating memory dynamically to elements and accessing a list of items on the basis of an index etc. These classes create collections of objects of the Object class, which is the base class for all data types in C#.Collections are enhancement to the arrays.
There are two distinct collection types in C#. The standard collections, which are found under the System.Collections namespace and the generic collections, under System.Collections.Generic. The generic collections are more flexible and are the preferred way to work with data. The generic collections or generics were introduced in .NET framework 2.0. Generics enhance code reuse, type safety, and performance.

various commonly used classes of the System.Collection namespace

ArrayList
Hashtable
SortedList
Stack
Queue
BitArray
LIST
dictionary

ArrayList--It represents ordered collection of an object that can be indexed individually. It is basically an alternative to an array. However unlike array you can add and remove items from a list at a specified position using an index and the array resizes itself automatically. It also allows dynamic memory allocation, add, search and sort items in the list


Hashtable--It uses a key to access the elements in the collection.A hash table is used when you need to access elements by using key, and you can identify a useful key value. Each item in the hash table has a key/value pair. The key is used to access the items in the collection.

BitArray--It represents an array of the binary representation using the values 1 and 0.It is used when you need to store the bits but do not know the number of bits in advance. You can access items from the BitArray collection by using an integer index, which starts from zero.

LIST--List is a strongly typed list of objects that can be accessed by index. It can be found under System.Collections.Generic namespace.

dictionary--dictionary, also called an associative array, is a collection of unique keys and a collection of values, where each key is associated with one value. Retrieving and adding values is very fast. Dictionaries take more memory, because for each value there is also a key.
Dictionary<string, string> domains = new Dictionary<string, string>();
domains.Add("de", "Germany");




Non-Generic collections - These are the collections that can hold elements of different data types. It holds all elements as object type.
So it includes overhead of type conversions.

Generic collections - These are the collections that can hold data of same type and we can decide what type of data that collections can hold.

Some advantages of generic collections - Type Safe, Secure, reduced overhead of type conversions.
// Generic collection
List<T> list = new List<T>();
list.Add("Any object");

// Non generic collection
List list = new List();
list.Add("Any object");

No comments:

Post a Comment