Non-Generic Collection in C#

Stack,Queue,LinkedList,SortedList,ArrayList,HashTable

A Collection is also similar to Array which is dynamic in size i.e. the size of a collection increases as you add new items to the collection whereas the arrays are fixed length which can only a Pre-defined number of Rows.

.NET provides us a number of classes implemented as collections under the namespace 'System.Collections' like Stack, Queue, Linked List, SortedtList, ArrayList, Hashtable, etc. which can be directly consumed.

Stack

 'Push' method adds a value from the bottom of the Stack.
 'Pop' method returns and removes the topmost value of the Stack.
 'Peek' method returns the topmost value of the Stack without removing it.
 'Clear' method clears all the values of the Stack.
 'Count' is a property that tells the number of items in the Stack.
using System;
using System.Collections;

namespace CollectionInCSharp
{
    class StackDemo
    {
        static void Main(string[] args)
        {
            Stack s = new Stack();
            s.Push(10);
            s.Push("Hello");
            s.Push(true);
            s.Push('a');
            s.Push(3.14f);
            s.Push(78.98);
            Console.WriteLine("Push Operation:");
            foreach (object obj in s)
            {
                Console.Write(obj + " "); //78.98 3.14 a True Hello 10
            }
            Console.WriteLine();


            Console.WriteLine("POP Operation:");
            Console.WriteLine(s.Pop()); //78.98

            Console.WriteLine("Stack after POP Operation:");
            foreach (object obj in s)
            {
                Console.Write(obj + " ");//3.14 a True Hello 10
            }
            Console.WriteLine();

            Console.WriteLine("Peek Operation:");
            Console.WriteLine(s.Peek()); //3.14

            Console.WriteLine("Stack after Peek Operation:");
            foreach (object obj in s)
            {
                Console.Write(obj + " ");//3.14 a True Hello 10
            }
            Console.WriteLine();

            Console.WriteLine("Count Operation:");
            Console.WriteLine(s.Count); //5
            s.Clear();
            Console.WriteLine(s.Count); //0

            Console.ReadLine();
        }
    }
}

ArrayList

When we want to transfer a set of values from one class to another we transfer them by storing 
the values under an ArrayList because this will be easier for transporting multiple values at a time.

Every collection class has the capability of incrementing its capacity dynamically whenever the 
requirement comes.

A collection class object can be created making use of 3 different constructors.


1) A default constructor which initializes the class within the initial capacity of '0' and that becomes 
4 after adding the first element under the collection from now whenever there is a 
requirement the capacity doubles.

2) We can also create the object of the collection class by specifying the initial capacity using the 
constructor. Now also whenever the requirement comes the capacity doubles.

3) We can create a collection by passing an existing collection as a parameter to the constructor 
which will copy the values of the old collection to the new collection which will be taken as an 
initial capacity. Now also when the requirement comes the capacity doubles.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CollectionInCSharp.Collections
{
    class ArrayListDemo
    {
        static void Main()
        {
            ArrayList al = new ArrayList();
            Console.WriteLine("Initial Capacity:" + al.Capacity);//0
            al.Add(100);
            Console.WriteLine("Capacity after adding 1st item:" + al.Capacity);//4
            al.Add(3.14f);
            al.Add(22.32m);
            al.Add(true);
            Console.WriteLine("Capacity after adding 4th item:" + al.Capacity);//4
            al.Add(false);
            Console.WriteLine("Capacity after adding 5th item:" + al.Capacity);//8
            al.Add("Hello");

            Console.WriteLine("Iterating ArrayList:");
            foreach (object obj in al)
            {
                Console.Write(obj + " ");//100 3.14 22.32 True False Hello
            }

            Console.WriteLine();
            Console.ReadLine();
        }
    }
}

HashTable

1. It is also a collection class that can store you values in the form of 'Key Value Pairs'.
2. Hashtable class is used for transporting of the data from one class to other in a descriptive 
fashion that is each value can be described by its key.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CollectionInCSharp.Collections
{
    class HashTableDemo
    {

        static void Main()
        {
            Hashtable ht = new Hashtable();
            ht.Add("Eno", 101);
            ht.Add("Ename", "Praveen");
            ht.Add("Job", "Manager");
            ht.Add("Salary", 50000);
            ht.Add("Dname", "Software");
            foreach (object obj in ht.Keys)
            Console.WriteLine(obj + ":\t" + ht[obj]);
             /* Output
Ename:  Md Asif Alam
Job:    Developer
Eno:    101
Dname:  Software
Salary: 50000
            */
            Console.ReadLine();
        }
    }
}