Provides a collection similar to an array, but that grows dynamically as the number of elements change.
Example
static void Main()
{
ArrayList list = new ArrayList();
list.Add(1);
list.Add(2);
list.Add(3);
foreach(int num in list)
{
Console.WriteLine(num);
}
}
Output
1
2
3
Stack:
A collection that works on the Last In First Out (LIFO) principle,Push - To add element and Pop – To Remove element
Example
class Test
{
static void Main()
{
Stack stack = new Stack();
stack.Push(2);
stack.Push(5);
stack.Push(8);
while(stack.Count != 0)
{
Console.WriteLine(stack.Pop());
}
}
}
Output
8
5
2
Queue:
A collection that works on the First In First Out (FIFO) principle, Enqueue: To add element and Dequeue:To Remove element
Example:
static void Main()
{
Queue queue = new Queue();
queue.Enqueue(1);
queue.Enqueue(2);
queue.Enqueue(3);
while(queue.Count != 0)
{
Console.WriteLine(queue.Dequeue());
}
}
Output
1
2
3
Dictionary:
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 organized
based on the hash code of the key.
Example:
static void Main()
{
Hashtable h1 = new Hashtable(20);
h1.Add("key1", "val1");
h1.Add("key2", "val2");
h1.Add("key3", "val3");
foreach(string key in h1.Keys)
{
Console.WriteLine(key);
}
}
Output
val1
val2
val3
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(8, "eight");
sl.Add(5, "five");
sl.Add(11, "eleven");
sl.Add(2, "two");
for(int i=0; i
Console.WriteLine("\t {0} \t\t {1}", sl.GetKey(i),
sl.GetByIndex(i));
}
}
Output
2 two
5 five
8 eight
11 eleven
No comments:
Post a Comment