C# hashtable

C# Hashable is a collection class that can be used to store pairs of key/value. When storing in the Hashtable object, key and value are converted to object type automatically. The most commonly used methods and properties of the Hashtable are :
-Add(object key, object value)
-Remove(object key)
-CopyTo(Array array, int start_index)

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

namespace Csharp_tip

   class Program
     static void Main(string[] args)
        //create Hashtable object
        Hashtable hb = new Hashtable();
        //Add key/value pairs to the Hashtable object
        try //catch key exist error
           hb.Add("c1", "Canada");
           hb.Add("c2", "USA");
           hb.Add("c3", "Autralia");
        catch (ArgumentException ae) { Console.WriteLine(ae.ToString() ); }
        //Read all values in the Hastable object using key
        foreach (object key in hb.Keys)
        //an alternative to read all values is using values collection property
        foreach (object value in hb.Values)
        //count all pairs of key and value
        Console.WriteLine("Number of key/value pairs:{0}",hb.Count );
        //key checking
        if (hb.ContainsKey("c3"))
           Console.WriteLine("This key exists.");
        //Copy all values to an array
        object []arrval=new object[hb.Count ];
        hb.Values.CopyTo(arrval, 0);
        //Copy all keys to an array
        object[] arrkey = new object[hb.Count];
        hb.Keys.CopyTo(arrkey, 0);

        //Remove a specific pair of key/value using key

        //Remove all pairs in the Hashtable object



Posted by: Lavy | post date: 01-28-2013 | Subject: C# Programming Language

This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.