Thursday 29 August 2013

Collection in c#.

Collection provide a more flexible way to work with group of objects. Unlike arrays , the group of objects you work with can grow and shrink  dynamically as the needs of the application change.

Collection is a class , so you must declare a new collection before you can add elements to that collection.

Collection classes are specialized classes for data storage and retrieval. Collection classes provide for stack ,queues,list and hash tables.

Collection classes are various purposes , such as allocating memory dynamically to element and accessing a list of items on the basis of an index etc.

For using the Collection , You have to add System.Collections namespace in your coding.

Types of collection classes and there Usage:-




Class
Description and Usage
ArrayList
It is an alternative to array. However unlike array you can add and remove items from a list of specified postion using an index and the array resize itself automatically. It allows dynamic memory allocation,
Add, search and sort items in the list.
HashTable
A hash table is used when you need to access an element using key. Each item in a hash table has a key/value pair. The key is used to access the item in collection.
SortedList
It is the combination of Arraylist and hashTable . It contains a list of items that can be accessed by using a key or an index. If you access items using an index , it is an arraylist ,and if you are accessing an item using a key , it is a hashtable. The collection of items is always sorted by the key value.
Stack
It represents a last-in , first out collection of objects.
It is used when you want a last in ,first out access of items. When you add an item in the list , it is called pushing the item and when you remove it , it is called poping the item.
Queue
It represents a first in, first out collection of object.
It is used when you want a first in, first out access of items. When you add an item in the list , it is called enqueue and when you remove an item , it is called dequeue.
BitArray
It is represent an array of the binary representation using the values 1 and 10.
It is used when you want 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 start from 0(Zero).

No comments:

Post a Comment

Note: only a member of this blog may post a comment.