- The size of an array is always fixed and must be defined at the time of instantiation of an array.
- An array can only contain objects of the same data type, which we need to define at the time of its instantiation.
So,In .net the concept of collections is very usefull to overcome from this limitaions.
The Namespace for Collections is
using System.Collections
.net 2.0 provides collections which are as described below.
- ArrayList
ArrayList Provides the collection similar to Arrays,but it grows dynamically as the number of Elements change.
Example
static void Main()
{
ArrayList list = new ArrayList();
list.Add(11);
list.Add(22);
list.Add(33);
foreach(int num in list)
{
Console.WriteLine(num);
}
}
Output
11
22
33
- Stack
A collection that works on the Last In First Out (LIFO) principle,
i.e., the last item inserted is the first item removed from the collection.
Push - To add element and
Pop – To Remove element
Example
class Test
{
static void Main()
{
Stack stack = new Stack();
stack.Push(2);
stack.Push(4);
stack.Push(6);
while(stack.Count != 0)
{
Console.WriteLine(stack.Pop());
}
}
}
Output
6
4
2
- Queue
A collection that works on the First In First Out (FIFO) principle,
i.e.,the first item inserted is the first item removed from the collection.
Enqueue - To add element
Dequeue – To Remove element
Example:
static void Main()
{
Queue queue = new Queue();
queue.Enqueue(2);
queue.Enqueue(4);
queue.Enqueue(6);
while(queue.Count != 0)
{
Console.WriteLine(queue.Dequeue());
}
}
Output
2
4
6
- Dictionaries
Dictionaries are a kind of collection that store items in a key-value pair fashion.
- HashTable
Provides a collection of key-value pairs that are organizedbased on the hash code of the key.
Example:
static void Main()
{
Hashtable ht = new Hashtable(20);
ht.Add("key", "Hello");
ht.Add("key1", "Hello1");
ht.Add("key2", "Hello2");
Console.WriteLine("Printing Keys...");
foreach(string key in ht.Keys)
{
Console.WriteLine(key);
}
Console.WriteLine("\nPrinting Values...");
foreach(string Value in ht.Values)
{
Console.WriteLine(Value);
}
Console.WriteLine("Size of Hashtable is {0}", ht.Count);
Console.WriteLine(ht.ContainsKey("key"));
Console.WriteLine(ht.ContainsValue("Hello"));
Console.WriteLine("\nRemoving element with key = key1");
ht.Remove("key1");
Console.WriteLine("Size of Hashtable is {0}", ht.Count);
}
Output
Printing Keys...
key
key1
key2
Printing Values...
Hello
Hello1
Hello2
Size of Hashtable is 3
True
True
Removing element with key = key1
Size of Hashtable is 2
- SortedList
Provides a collection of key-value pairs where the items are sorted according to the key.
The items are accessible by both the keys and the index.
Example:
static void Main()
{
SortedList sl = new SortedList();
sl.Add(18, "Java");
sl.Add(5, "C#");
sl.Add(11, "VB.Net");
sl.Add(1, "C++.Net");
Console.WriteLine("The items in the sorted order are...");
Console.WriteLine("\t Key \t\t Value");
Console.WriteLine("\t === \t\t =====");
for(int i=0; i
{
Console.WriteLine("\t {0} \t\t {1}", sl.GetKey(i),sl.GetByIndex(i));
}
}
Output
Key Value
==
1 C++.Net
5 C#
11 VB.Net
18 Java
No comments:
Post a Comment