3
Answers

How to swap Hashtable's key with Value.

Suhas  D

Suhas D

11y
2.3k
1
How to swap Hashtable's key with Value.
I have solved it by following.
 
Is there any other solution?

class Program
    {
        static void Main(string[] args)
        {
            Hashtable hsTbl = new Hashtable();

            hsTbl.Add(1, "Suhas");
            hsTbl.Add(2, "Madhuri");
            hsTbl.Add(3, "Om");
            List<object> keyList = new List<object>();
            List<object> ValList = new List<object>();

            Console.WriteLine("Key          Value");
            foreach (DictionaryEntry item in hsTbl)
            {
                Console.WriteLine(item.Key + "      " + item.Value);
                keyList.Add(item.Value);
                ValList.Add(item.Key);
            
             }
            hsTbl.Clear()

//Swapping          

  for (int i = 0; i < keyList.Count; i++)
            {
                hsTbl.Add(keyList[i], ValList[i]);
            }

//will display hashtable after swapping

 
            foreach (DictionaryEntry item in hsTbl)
            {
                Console.WriteLine(item.Key + "      " + item.Value);

            }
  }
 }





Answers (3)