Wednesday, July 30, 2014

What is Collection in .Net?

Collection classes are the specialized classes for data storage and retrieval.

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 collection of objects of the Object class, which is the base class for all data types in C#.


Most of the times we get a requirement to store the multiple data. This can be done by using the Array which supports the data storage at different indexes created. An Array is a collection of multiple data at different indexes. However this is a form of strongly typed object, we can store information in array of same type of object. 

On the other hand Collections gives us the flexibility to store the data of different type of objects in it.

Namespace for collections: System.Collection
Various types of collections and their usage.


Class
Usage
ArrayList
It represents an ordered list of objects which can be retrieved by indexes.
An Arraylist is alternative to an Array. However unlike array you can Add, Remove the items from the list and can be retrieved by indexes. An ArrayList is not bound to any specific length
Hashtable
This collection can store the objects with a key assigned to it. Any data storage is in the form of key-value pair in Hashtable and the stored object can be retrieved by the key which is assigned to it.
Stack
It represents a last-in and first out collection of object. When the item is added to stack, it is called “pushing” the item and when it get removed, it is called as “popping” the item.
Queue
It represents a first-in and first out collection of object. When the item is added to stack, it is called “enqueue” the item and when it get removed, it is called as “dequeue” the item.
SortedList
A SortedList is a combination of an array and a Hashtable.
It uses a key as well as an index to access the items in a list. If you access the items using an index, it is an Arraylist and if you access the items using a key, it is a Hashtable. The collection of items is always sorted by the key value.

No comments:

Post a Comment

Put your comments here

Motivational qoutes

पूरे विश्वास के साथ अपने सपनों की तरफ बढ़ें। वही ज़िन्दगी जियें जिसकी कल्पना आपने की है।