Thursday, May 23, 2013

Collection in C# with Example


Collection in C#
Collection classes are specialized classes for data storage and retrieval. These classes provide support for
Stacks
 Queues
 Lists
hash tables
Most collection classes implement the same interfaces.
Collection classes serve various purposes, such as allocating memory dynamically to elements and accessing a list of items on the basis of an index etc. These classes create collections of objects of the Object class, which is the base class for all data types in C#.

Stack Class
Stack handles last in first out data processing mechanism. Stack class in one of Collection class of .NET Class library. You have to include using System.Collections.Generic; namespace in order to use collection class in your program.

Create object of collection class like this

            Stack<Int32> st = new Stack<int>();
And you can call push() or pop() method of collection class in order to insert data and delete data symulteniously.

In this below example I have created one object of collection class which is (st). And I have inserted two element in my Stack class. Then I have called st.Pop(); to delete top item of my Stack Collection. Then I am displaying the top most item by calling Peek() function.

Console.Write(st.Peek());
And as I have deleted my top most item from stack now my top item is 100.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BlogProject
{
    class Program
    {
        static void Main(string[] args)
        {

            Stack<Int32> st = new Stack<int>();
            st.Push(100);
            st.Push(200);
            st.Pop();
            Console.Write(st.Peek());
            Console.ReadLine();
        }
    }
}



Queue Class
Queue handles first in first out data processing mechanism. queue class in one of Collection class of .NET Class library. You have to include using System.Collections.Generic; namespace in order to use collection class in your program.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BlogProject
{
    class Program
    {
        static void Main(string[] args)
        {
            Queue<Int32> qu = new Queue<int>();
            qu.Enqueue(100);
            qu.Enqueue(200);
            qu.Dequeue();

            Console.WriteLine(qu.Last());
            Console.ReadLine();

        }
    }
}


Create object of Queue class like this

            Queue<Int32> qu = new Queue<int>();
You can call qu.Enqueue(); function to insert value into your queue.
You can call qu.Dequeue(); function to delete item from your queue. It will delete the first item of the queue what you have inserted.

Now in this program I have inserted two value at first and deleted one item. And by nature the first item(100) will get delete.

And ,then the last tem of my Queue is 200. I can get last item of Queue by calling qu.Last() function.
           





List Class
List class in one of Collection class of .NET Class library. You have to include using System.Collections.Generic; namespace in order to use collection class in your program.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BlogProject
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Int32> lst = new List<int>();
            lst.Add(100);
            lst.Add(200);
            lst.Remove(100);
            Console.WriteLine(lst[0]);
            Console.ReadLine();

        }
    }
}





Create object of List class like this
List<Int32> lst = new List<int>();
To insert data in list object use Add() function. Like below.
lst.Add(100);

To remove particular object from list we have to use Remove() function of List class object. Like

lst.Remove(100);

to get any index value use lst[index]. That is object-name[index]

I have given simple code in below.






Hash Table
HashTable  class is one of Collection class of .NET Class library. You have to include
using System.Collections;
 namespace in order to use collection class in your program.
HashTable use key value pair to store data in Object. In HashTable it’s very easy to search any value as HashTable maintain key to identify specific value.

Here i have given full example of hash table
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace BlogProject
{
    class Program
    {
        static void Main(string[] args)
        {

            Hashtable hashtable = new Hashtable();
            hashtable[1] = "One";
            hashtable[2] = "Two";
            hashtable[13] = "Thirteen";

            foreach (DictionaryEntry entry in hashtable)
            {
                Console.WriteLine("{0}, {1}", entry.Key, entry.Value);
            }

            Console.ReadLine();

        }
    }
}


Here within for each loop I am displaying all value of item using key.

No comments:

Post a Comment