Collections in .NET Framework.
- What is collections?
- Needs of Collections/ importance of collection
- various most useful classes of collection such as
ArrayList, Hashtable, Stack, Queue, BitArray, SortedList
3. Introduction
Collection means a group of different types of
data.
It provides automatic memory management and
capacity expansion.
The System.Collections namespace provides
various classes such as ArrayList, Stack,
Queue, Hashtable, SortedList and BitArray.
Some collections classes provides search and
sort capabilities on the basis of index values or
associated key values of the elements.
4. 1. ArrayList
• It represents ordered collection of an object that
can be indexed individually.
• It is an alternative of an array. Unlike array, you
can
– Add or remove items from a specified position using
an index.
– Array resizes itself automatically.
• It also allows dynamic memory allocation, adding,
searching and sorting items in the list.
5. 1. ArrayList
• Properties of ArrayList:
Property Description
Capacity Gets or sets the number of elements that the ArrayList
can contain.
Count Gets the number of elements actually contained in the
ArrayList.
IsFixedSize Gets a value indicating whether the ArrayList has a fixed size.
IsReadOnly Gets a value indicating whether the ArrayList is read only.
Item Gets or sets the element at the specified index.
6. Example:
using system;
using system.collections;
namespace CollectionApp
{ class Program
{ static void Main(string[] args)
{
ArrayList a1=new ArrayList();
a1.add(104);
a1.add(101);
a1.add(103);
Console.WriteLine(“Capacity:”,a1.Capacity);
Console.WriteLine(“Count:”,a1.Count);
foreach ( int i in a1) { Console.Write(i+” “); }
Console.WriteLine(“Sorted Contens”);
a1.Sort();
foreach ( int i in a1) { Console.Write(i+” “); }
Console.ReadLine();
}
}
}
7. 2. Hashtable
• It is used when you need to access elements by using
key.
• Each item in the Hashtable has key/value pair. This
key is used to access the items in the collection.
8. 2. Hashtable
• Properties of Hashtable:
Property Description
Count Gets no. of key/value pair contained in the table.
IsFixedSize Gets a value indicating whether the Hashtable has a fixed size.
IsReadOnly Gets a value indicating whether the Hashtable is read only.
Item
Gets or sets the value associated with the specified key.
Keys
Gets an ICollection containing the keys in the Hashtable.
Values Gets an ICollection containing the values in the Hashtable.
9. Example:
using system;
using system.collections;
namespace CollectionApp
{ class Program
{ static void Main(string[] args)
{
Hashtable ht=new Hashtable();
ht.add(“111”,”AAA”);
ht.add(“222”,”BBB”);
ht.add(“333”,”CCC”);
if(ht.ContainsValue(“DDD”))
{ Console.WriteLine(“Already is in table”); }
else
{ ht.Add(“444”,”DDD”); }
ICollection key = ht.Keys;
foreach ( string k in key) { Console.Write(k+”:”+ht[k]); }
Console.ReadLine();
}
}
}
10. 3. SortedList
• A SortedList is a combination of ArrayList and
hashtable.
• It uses a key as well as an index to access the items
in a list.
• The collection of items is always sorted by the key
value.
11. 3. SortedList
• Properties of SortedList:
Property Description
Capacity Gets or sets the capacity of Sortedlist
Count Gets the no. of elements contained in the SortedList.
IsFixedSize Gets a value indicating whether the SortedList has a fixed size.
IsReadOnly Gets a value indicating whether the SortedList is read only.
Item
Gets or sets the value associated with the specified key in
SortedList.
Keys
Gets the keys in the SortedList.
Values Gets the values in the SortedList.
12. Example:
using system;
using system.collections;
namespace CollectionApp
{ class Program
{ static void Main(string[] args)
{
SortedList sl=new SortedList();
sl.add(“111”,”AAA”);
sl.add(“222”,”BBB”);
sl.add(“333”,”CCC”);
if(sl.ContainsValue(“DDD”))
{ Console.WriteLine(“Already is in table”); }
else
{ sl.Add(“444”,”DDD”); }
ICollection key = sl.Keys;
foreach ( string k in key) { Console.Write(k+”:”+sl[k]); }
Console.ReadLine();
}
}
}
13. 4. Stack
• It represents last-in, first-out collection of object.
• It is used when you need a last-in, first-out access of
items.
• Operations on Stack:
1. Push() : Adding an item to the stack.
2. Pop() : Removing an item from stack.
• Properties of Stack:
14. Example:
using system;
using system.collections;
namespace CollectionApp
{ class Program
{ static void Main(string[] args)
{
Stack st = new Stack();
st.Push(‘A’);
st.Push(‘M’);
st.Push(‘G’);
st.Push(‘W’);
Console.WriteLine(“Current Stack:”);
foreach( char c in st ) { Console.Write( c + “ “); }
Console.WriteLine(“Removing items”);
st.Pop();
st.Pop();
Console.WriteLine(“Current Stack:”);
foreach( char c in st ) { Console.Write( c + “ “); }
}
} }
15. 5. Queue
• It represents first-in, first-out collection of object.
• It is used when you need a first-in, first-out access of
items.
• Operations on Queue:
1. Enqueue : Adding an item to queue.
2. Dequeue : Removing an item from queue.
• Properties of Queue:
16. Example:
using system;
using system.collections;
namespace CollectionApp
{ class Program
{ static void Main(string[] args)
{
Queue q = new Queue();
q.Enqueue(‘A’);
q.Enqueue(‘M’);
q.Enqueue(‘G’);
q.Enqueue(‘W’);
Console.WriteLine(“Current Queue:”);
foreach( char c in q ) { Console.Write( c + “ “); }
Console.WriteLine(“Removing items”);
char ch = (char)q.Dequeue();
Console.WriteLine(“Removed”,ch);
char ch = (char)q.Dequeue();
Console.WriteLine(“Removed”,ch);
Console.WriteLine(“Current Queue:”);
foreach( char c in q) { Console.Write(c+ “ “); } }}}
17. 6. BitArray
• It represents an array of binary representation using
the values 0’s and 1’s.
• It is used when you need to store the bits but you don’t
know the no. of bits in advance.
• You can access items from BitArray collection by
using an integer index, which starts from 0.
18. 6. BitArray
• Properties of BitArray:
Property Description
Count Gets the no. of elements contained in the BitArray.
IsReadOnly Gets a value indicating whether the BitArray is read only.
Item
Gets or sets the value of the bit at a specific position in
BitArray.
Length Gets or sets the number of elements in the BitArray.
19. Example:
using system;
using system.collections;
namespace CollectionApp
{ class Program
{ static void Main(string[] args)
{
BitArray b = new BitArray(8);
byte [] a = { 11 };
b = new BitArray(a);
Console.WriteLine(“BitArray b for a=11:”);
for( int i=0; i<b.Count; i++)
{
Console.WriteLine(b[i]);
}
}
}
}