convert HashTable to Dictionary in C#

前端 未结 5 1799
闹比i
闹比i 2020-12-15 03:23

how to convert a HashTable to Dictionary in C#? is it possible? for example if I have collection of objects in HashTable and if I want to convert it to a dictionary of objec

5条回答
  •  旧巷少年郎
    2020-12-15 04:30

        Hashtable openWith = new Hashtable();
        Dictionary dictionary = new Dictionary();
    
        // Add some elements to the hash table. There are no 
        // duplicate keys, but some of the values are duplicates.
        openWith.Add("txt", "notepad.exe");
        openWith.Add("bmp", "paint.exe");
        openWith.Add("dib", "paint.exe");
        openWith.Add("rtf", "wordpad.exe");
    
        foreach (string key in openWith.Keys)
        {
            dictionary.Add(key, openWith[key].ToString());
        }
    

提交回复
热议问题