Collection in C#

By
Collection classes  are basically  used for data retrieval and data storage purpose.It is also used for Dynamic memory allocation and indexing purpose.Through Indexing property we can retrieve data very fast.Collection is the improvement of array in which we can store any types of data and improving the functionality of the array like:
  • Fixed size of array.
  • Insertion problem in array.
  • Similar type of Data type .
Types of array in collection:
There are some types of array in collection.
  1. Array List --> Array List is an alternative of the array.You can easily insert and remove elements using index in Array List.In Array List you can easily apply dynamic memory allocation ,sorting,searching etc. 
  2. Hash Table --> In hash table we can easily access any element(value) on the basis of key.Each element in hash table  has a key and value pair.
  3. Stack --> Stack is basically used for Last In First Out(LIFO).
  4. Queue--> Queue is used for First In First Out(FIFO).
Note->There are some other types of array Sorted List,Bit Array .These are used rarely in array.
Types of Collection :-
There are three types of collection in c#. which is given below:
  1. Non Generic Collection
  2. Generic Collection
  3. Specialized collection
Now First,I am going to learn about Non Generic collection in c# .
     1. Non Generic Collection:- The value will store in Object Type .So we have the requirement of Boxing and Unboxing in this collection.Due Boxing and Unboxing  processing speed of program is slow.
There are some example of Non Generic collection in c#.Now i am going to explain Non Generic Collection in C# using programming.
      1. Array List :
using System;
using System.Collections;
namespace Non_Generic_collection
{
    class Program
    {
        static void Main(string[] args)
        {
            ArrayList li = new ArrayList();
            li.Add("Ram");
            li.Add("shyam");
            li.Add(12);
            li.Add(30);
            li.Add('a');
            li.Add(15.50);   
            int i = li.Count;
            Console.WriteLine( "Number of element in arrayList is " +i);
            li.TrimToSize();//sets the capacity to the actual number of element in the System.collection ArrayList.
            int j = li.Capacity;
            Console.WriteLine("capacity of array element is " +j);
            li.Remove(12);
            foreach (object m in li)
            {
                Console.WriteLine("element arraylist after removal is " +m);
            }
            li.Reverse();
            foreach (object r in li)
            {
                Console.WriteLine("element after reverse is " + r);
            }
            Console.ReadLine();     
        }
    }
}
Output:-

 Description:- In this example i have created an Array List with object li [Ex. ArrayList li = new ArrayList()].In for each loop value is stored in only Object Type. To solve this Boxing and Unboxing problem ,Generic Collection or Specialized  Collection Classes  are used.

      2. Stack :
using System;
using System.Collections;
namespace stack
{
    class Program
    {
        static void Main(string[] args)
        {
            Stack st = new Stack();
            st.Push("abc");
            st.Push("Ram");
            st.Push("sohan");
            st.Push(12);
            st.Push(15.5);
            foreach (object s in st)
            {
                Console.WriteLine("stack element is " + s);
            }
            int i = st.Count;
            if (i >4)
            {
                st.Pop();//used for remove the value in stack.
                foreach (object m in st)
                {
          Console.WriteLine("atfer deletion remaining element in stack is " +m);
                }
            }
            Console.ReadKey();
        }
    }
}
Output:


Description:-In this above example i have created a stack with object st (Ex.  Stack st = new Stack()).

     3. Queue :
using System;
using System.Collections;
namespace queue1
{
    class Program
    {
        static void Main(string[] args)
        {
            Queue qu = new Queue();
            qu.Enqueue("Ram");
            qu.Enqueue("kamal");
            qu.Enqueue("vaibhav");
            qu.Enqueue(123);
            foreach (object q in qu)
            {
                Console.WriteLine("element in queue is " +q);
            }
          int i= qu.Count;
          Console.WriteLine("Number element in queue is " +i);
          if(i>2)
          {
              try
              {
                  Console.WriteLine(qu.Dequeue());
                  foreach (object n in qu)
                  {
                      Console.WriteLine("element in queue after dequeue is " +n);
                      Console.WriteLine("no element in queue");
                  }
              }
              catch(Exception e)
              {
                  Console.WriteLine(e.Message);
              }
          }
          Console.ReadLine();
        }
    }
}
Output:


Description:- In this above example i have created a Queue with qu object (Ex. Queue qu = new Queue() ).

4. Hash Table:
using System;
using System.Collections;
namespace hashtable
{
    class Program
    {
        static void Main(string[] args)
        {
            Hashtable ht = new Hashtable();
            ht.Add(101, "Ram");
            ht.Add(102, "Rajesh");
            ht.Add(103, "vijay");
            Console.WriteLine(ht[103]);
            Console.WriteLine();
            foreach (DictionaryEntry di in ht)
            {
                Console.WriteLine("key value is " + di.Key);
                Console.WriteLine("and corresponding value is " + di.Value);
            }
            Console.WriteLine();
            ht.Remove(101);
            foreach (DictionaryEntry dict in ht)
            {
               Console.WriteLine("key value is " + dict.Key);
               Console.WriteLine("and corresponding value is " + dict.Value);
            }
            Console.ReadLine();
        }
    }
}
Output:

Description:- In this above example i have created a Hash Table with ht object(Ex. Hashtable ht = new Hashtable()).All other things are same as above example.

Note:- For Non Generic Collection Name space is used: 
using System.Collections;
To solve the Type Conversion problem a new collection came which is known as Generic Collection.
For more

  1. Partial Classes in C#
  2. Param Keyword in C#

I hope this helpful for you.
To Get the Latest  Free Updates Subscribe
Click below for download whole Application
            Download
After downloading the application Run this application one by one.For this Open Solution Explorer-->Right click on the project(which you want to run)-->Set as StartUp Project-->Run(Press F5).

0 comments:

Post a Comment

Powered by Blogger.