convert HashTable to Dictionary in C#

前端 未结 5 1797
闹比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:29

    Extension method version of agent-j's answer:

    using System.Collections;
    using System.Collections.Generic;
    using System.Linq;
    
    public static class Extensions {
    
        public static Dictionary ToDictionary (this Hashtable table)
        {
           return table
             .Cast ()
             .ToDictionary (kvp => (K)kvp.Key, kvp => (V)kvp.Value);
        }
    }
    

提交回复
热议问题