哈希表转换到词典在C# [英] convert HashTable to Dictionary in C#

查看:237
本文介绍了哈希表转换到词典在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将一个哈希表转换为词典在C#中?可能吗?举例来说,如果我有收集对象的哈希表,如果我想将其转换为对象的字典,一个特定的类型,那怎么办?

解决方案

 公共静态字典< K,V> HashtableToDictionary< K,V> (哈希表的表)
{
   返回表
     .Cast<的DictionaryEntry> ()
     .ToDictionary(KVP =>(K)kvp.Key,KVP =>(五)kvp.Value);
}
 

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 objects with a specific type, how to do that?

解决方案

public static Dictionary<K,V> HashtableToDictionary<K,V> (Hashtable table)
{
   return table
     .Cast<DictionaryEntry> ()
     .ToDictionary (kvp => (K)kvp.Key, kvp => (V)kvp.Value);
}

这篇关于哈希表转换到词典在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆