Professional Documents
Culture Documents
Collections
Collections
Collections
Namespace
The .NET framework provides specialized classes for data storage
and retrieval.
For many applications, you want to create and manage groups of related
objects. There are two ways to group objects: by creating arrays of
objects, & by creating collections of objects.
Arrays are most useful for creating and working with a fixed no. of
strongly-typed objects.
Collections provide a more flexible way to work with groups of objects.
Unlike arrays, the group of objects you work with can grow and shrink
dynamically as the needs of the application change. For some collections,
you can assign a key to any object that you put into the collection so that
you can quickly retrieve the object by using the key.
A collection is a class, so you must declare an instance of the class before
you can add elements to that collection. If your collection contains
elements of only one data type, you can use one of the classes in the
System.Collections.Genericnamespace.
Collection classes are specialized classes for data storage and retrieval which is an
enhancement for the Arrays. These classes provide support for stacks, queues, lists,
& hash tables. Most collection classes implement the same interfaces. 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 like array. These classes
create collections of objects of the Object class, which is the base class for all data
types in C#.
Using a simple
Collection
2.
System.Collections
Generic collections are more flexible and are the preferred way to
work with data.
Array List
HashTable
SortedList
Stack
Queue
BitArray
1. Array List
It represents ordered collection of an object that can be indexedindividually.
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 anindexand the array
resizes itself automatically.
It also allows dynamic memory allocation, adding, searching and sorting items
in the list.
2. Hash Table
It uses akeyto 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 akey/valuepair. The key is
used to access the items in the collection.
Hashtable
in
C#
represents a collection
of key/value pairs which
maps keys to value. Any
non-null object can be
used as a key but a
value
can.
retrieve
We
items
can
from
Both
keys
and
3. Sorted List
It uses akeyas well as anindexto access the items in a list.
A sorted list is a combination of an array and a hash table. It
contains a list of items that can be accessed using a key or an
index. If you access items using an index, it is an ArrayList, and if
you access items using a key , it is a Hashtable. The collection of
items is always sorted by the key value.
4. Stack
It represents alast-in, first outcollection of object.
It is used when you need a last-in, first-out access of items.
When you add an item in the list, it is calledpushingthe
item and when you remove it, it is calledpoppingthe item.
Note:
Astackis a Last-InFirst-Out (LIFO) data
structure.
The last element added
to the queue will be the
first one to be removed.
The C language uses a
stack to store local data
in a function. The stack is
also used when
implementing
calculators.
5. Queue
It represents afirst-in, first outcollection of object.
It is used when you need a first-in, first-out access of
items. When you add an item in the list, it is
calledenqueueand when you remove an item, it is
calleddeque.
6. BitArray
It represents an array of thebinary representationusing 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 aninteger index, which starts
from zero.
The BitArray class manages a compact array of bit values, which are represented as
Booleans, where true indicates that the bit is on (1) and false indicates the bit is off (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.