Sunday, 3 May 2015

How to use C# HashTable Class ?

You already have a System.Collections.Generic namespace, but a Hastable is not part of this collection - it's part of the normal Collections namespace.

Hashtable in C# represents a collection of key/value pairs which maps keys to value. Any non-null object can be used as a key but a value can. We can retrieve items from hashTable to provide the key . Both keys and values are Objects.

// Creating an object of HashTable
  Hashtable ht = new  Hashtable();


Operation on HashTable


   1.   Add:   Its use to add a pair of value in HashTable
      Syntex-         ht.Add(keyObj, valueObj)
      Example-      ht.Add(1, Monday)

   2.  ContainsKey:   Check if a specified key exist or not and retruns boolean value
          Syntex-             ht.ContainsKey(keyObj)
          Example-          bool  IsExist  =    ht.ContainsKey(1))
      Return value-  True

  3.  ContainsValue:   Check if a specified Value exist or not and retruns boolean value
          Syntex-             ht.ContainsValue(valueObj)
          Example-          bool  IsExist  =    ht.ContainsValue(Monday))
      Return value-  True
  
   4.  Remove:  Remove the specified Key and corresponding Value if exist
          Syntex -       ht.Remove(keyObj);
          Example-     ht.Remove(1);

 Source Code Example:-

         In below example I explained each above operation 


using System;
using System.Linq;
using System.Text;
using System.Globalization;
using System.Collections;

Namespace ConsoleDemoApp
{
    Class Test
    {
        /* program to implement Dictionay in c# */

        public static void Main(string[] args)
        {
            int inputNum=0;

          
            Console.Write("\nEnter number of day in a Week: ");
            inputNum= int.Parse(Console.ReadLine());

            HashTableExample ht = new HashTableExample();
            ht.HT_Operations(inputNum);
            Console.ReadKey();
        }
    }

  
  

    //
    Public class HashTableExample
    {
        Public void HT_Operations(int inputNum)
        {
            string dayName;
           
           Hashtable WeekDays = new  Hashtable();
           for(int i=0; i<inputNum; i++)
            {
                Console.Write("\nEnter {0} day of Week: ", i+1);
                dayName = Console.ReadLine();
                 WeekDays.Add(i,dayName);
                 Console.Write("\n");
            }

            // print all
            Console.Write("\nEntered Days are: \n");
            foreach(DictionaryEntry day in WeekDays)
            {
               Console.Write(day.Value);
               Console.Write("\n");
            }

            //print a single day 
            Console.Write(WeekDays[3].ToString());
            Console.Write("\n");


            //Search an Item by value
            Console.Write("\nEntered a Days for search: \n");
            dayName = Console.ReadLine();
            if (WeekDays.ContainsValue(dayName))
            {
                Console.Write("\n {0} is Found in the Dictionay: \n", dayName);
            }
            else
            {
                Console.Write("Not Found!");
            }


            //Search an Item by key
            Console.Write("\nEntered a key for search: \n");
           int key = int.Parse(Console.ReadLine());
           if (WeekDays.ContainsKey(key))
            {
                Console.Write("\nFor entered key {0} the day name is: {1} \n", key, WeekDays[key].ToString());
            }
            else
            {
                Console.Write("Not Found!");
            }

           //now remove an Item using key
            Console.Write("\nEntered a key to remove a Day: \n");
            key = int.Parse(Console.ReadLine());
            if (WeekDays.ContainsKey(key))
            {
                Console.Write("\nThe Day {0} has been removed: \n", WeekDays[key].ToString());
                WeekDays.Remove(key);
            }
            else
            {
                Console.Write("No Day exits at key:  {0}", key);
            }


            // print the remaining days
            Console.Write("\nRemaing Days are: \n");
            foreach(DictionaryEntry day in WeekDays)
            {
                Console.Write(day.Key + " - " + day.Value);
                Console.Write("\n");
            }
        }
    }
}


In above program I entered number of day in week is 7. After it will ask enter all day one by one 
and after that it will print instructions and ask relevant key/value to enter

Output:-
















Saturday, 2 May 2015

How to use C# ArrayList Class

ArrayList:-

Array lists are not strongly type collection. It will store values of different datatypes or same datatype. Array list size will increase or decrease dynamically it can take any size of values from any data type. These Array lists will be accessible with “System.Collections” namespace

ArrayList is one of the most flexible data structure from CSharp Collections. ArrayList contains a simple list of values. ArrayList implements the IList interface using an array and very easily we can add , insert , delete , view etc. It is very flexible because we can add without any size information , that is it will grow dynamically and also shrink .

Operations On ArrayList:-


 Add :
          Add method we use to append Items at the end of an ArrayList
  Insert :
          Insert method  we use Add Item in a specified position in an ArrayList
  Remove :
          Remove method we use to Item from ArrayList by specifying Item's name
  RemoveAt: 
          RemoveAt method we use to remove an item from a specified position
  Sort :
          To Sort Items in an ArrayList

Note :- All operation are operate on  the Object of ArrayList 

Source code Example



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

Namespace ConsoleDemoApp
{
    class Test
    {
        /* program to implement ArrayList in c# */

        public static void Main(string[] args)
        {
            int inputNum=0;

          
            Console.Write("\nEnter number of students to be add: ");
            inputNum= int.Parse(Console.ReadLine());

            ArrList arList = new ArrList();
            arList.ArrListOperations(inputNum);
            Console.ReadKey();
        }
    }

  
  

    Public class ArrList
    {
        public void ArrListOperations(int inputNum)
        {
          
            ArrayList ItemList = new ArrayList();
            for (int i=1; i<=inputNum; i++)
            {
            ItemList.Add("Item"+i);
            }

            // print
            Console.Write("\nEntered Items are: \n");
            for (int j = 0; j <= ItemList.Count-1; j++)
            {
               Console.Write(ItemList[j].ToString());
               Console.Write("\n");
            }

            //insert an item
            ItemList.Insert(3, "Item6");
            Console.Write("\nAfter inserting an item Item6 at 3 index: \n");
            for (int j = 0; j <= ItemList.Count - 1; j++)
            {
                Console.Write(ItemList[j].ToString());
                Console.Write("\n");
            }

            //sort itemms in an arraylist
            ItemList.Sort();
            Console.Write("\nAfter shorting ArrryList: \n");
            for (int j = 0; j <= ItemList.Count - 1; j++)
            {
                Console.Write(ItemList[j].ToString());
                Console.Write("\n");
            }


            //remove an item
            ItemList.Remove("Item1");
            Console.Write("\nAfter Removing item Item1: \n");
            for (int j = 0; j <= ItemList.Count - 1; j++)
            {
                Console.Write(ItemList[j].ToString());
                Console.Write("\n");
            }



            //remove item from a specified index
            ItemList.RemoveAt(3);
            // final ArrayList
            Console.Write("\n After Removing item at index 3, items are: \n");
            for (int j = 0; j <= ItemList.Count-1; j++)
            {
                Console.Write(ItemList[j].ToString());
                Console.Write("\n");
            }
        }
    }
}


Output:-