As we know, variable holds a single value. C# includes specialized that hold many values and objects. Collection classes are specialized classes for data storage and retrieval. These classes provide support for stacks, queues, lists, and hash tables
There are two types of collections available in C#: non-generic collections and generic collection
Non-Generic Generic
Non-Generic Generic
1. ArrayList 1. List
2. HashTable 2. Dictionary
3. SortedList 3. SortedList
4. Stack 4. Stack
5. Queue 5. Queud
Array:- Type Safe but fixed length
Collection:- Auto Resizing but not type safe
Generic Collection:- Auto Resizing and type safe
Generic Collection:- A generic collection is strongly typed (you can store one type of objects into it)
Example Code of each collections and Generic
class CollectionInCSharp
{
public static void Main()
{
Console.WriteLine("Non-Generic Generic");
Console.WriteLine("ArrayList----------->List");
Console.WriteLine("HashTable----------->Dictionary");
Console.WriteLine("SortedList---------->SortedList");
Console.WriteLine("Stack--------------->Stack");
Console.WriteLine("Queue--------------->Queue");
//Collections:- Varrying length but not fixed type
//ArrayList
Console.WriteLine("******************************Collection Examples******************************");
Console.WriteLine("------------Array List------------");
ArrayList al = new ArrayList();
al.Add("Parag");
al.Add(31);
al.Add("Nagpur");
al.Add(45000);
foreach (object o in al)
{
Console.WriteLine(o);
}
//HashTable
Console.WriteLine("------------Hash Table------------");
Console.WriteLine("DictonaryEntry: is a class whose object represents the data in a combination of key & value pairs");
Hashtable ht = new Hashtable();
ht.Add("Name", "Parag");
ht.Add("Age", 31);
ht.Add("City", "Nagpur");
ht.Add("Salary", 45000);
ht.Add("Married", "Yes");
foreach(DictionaryEntry d in ht)
{
Console.WriteLine(d.Key+":"+d.Value);
}
//SortedList
Console.WriteLine("------------Sorted List------------");
SortedList sl = new SortedList();
sl.Add("A", "Apple");
sl.Add("C", "Cat");
sl.Add("D", "Dog");
sl.Add("E", "Elephant");
sl.Add("B", "Ball");
foreach(DictionaryEntry d in sl)
{
Console.WriteLine(d.Key + ":" + d.Value);
}
//Stack
Console.WriteLine("------------Stack------------");
Stack s = new Stack();
s.Push("Bottle");
s.Push("Keyboard");
s.Push("Mouse");
s.Push(45000);
//s.Pop();
foreach (object o in s)
{
Console.WriteLine(o);
}
//Queue
Console.WriteLine("------------Queue------------");
Queue q = new Queue();
q.Enqueue("Mobile");
q.Enqueue("Bottle");
q.Enqueue(30000);
q.Enqueue("Keyboard");
//q.Dequeue();
foreach (object o in q)
{
Console.WriteLine(o);
}
//Generic:- Specific Type, ArraySize is not fixed, Elements can be added/removed at runtime
Console.WriteLine("************************************Generic Examples************************************");
Console.WriteLine("------------List<T>------------");
List<int> obj = new List<int>();
obj.Add(13);
obj.Add(08);
obj.Add(1988);
foreach (int i in obj)
{
Console.WriteLine(i);
}
Console.WriteLine("------------Dictionary<int,string>------------");
Dictionary<int, string> dict = new Dictionary<int, string>();
dict.Add(1, "Sunday");
dict.Add(2, "Monday");
dict.Add(3, "Tuesday");
dict.Add(4, "Wednesday");
dict.Add(5, "Thusday");
dict.Add(6, "Friday");
dict.Add(7, "Saturday");
foreach (KeyValuePair<int, string> kvp in dict)
{
Console.WriteLine(kvp.Key+":"+kvp.Value);
}
Console.WriteLine("------------SortedList<string,string>------------");
SortedList<string, string> sl1 = new SortedList<string, string>();
sl1.Add("A", "Apple");
sl1.Add("B", "Ball");
sl1.Add("C", "Cat");
foreach(KeyValuePair<string,string> kvp in sl1)
{
Console.WriteLine(kvp.Key+":"+kvp.Value);
}
Console.ReadKey();
}
}