我看到很多关于如何将散列表转换为字典的问题/答案,但是如何将字典转换为散列表?如何在C#中将Dictionary <>转换为Hashtable?
回答
最简单的方法是使用Hashtable的构造函数:
var dictionary = new Dictionary<object, object>();
//... fill the dictionary
var hashtable = new Hashtable(dictionary);
似乎相当罕见想做的事,但在简单的:
var hash = new Hashtable();
foreach(var pair in dictionary) {
hash.Add(pair.Key,pair.Value);
}
(假设没有不寻常“的工具类型平等检查,但不无类型平等检查”等)
现在只需在'ToHashTable'扩展方法中包含这个,瞧! – Dismissile 2012-03-29 14:50:54
甚至有一个[构造函数](http://msdn.microsoft.com/zh-cn/library/kdkta2sx.aspx)可以执行相同的操作。 – Joey 2012-03-29 14:55:37
这就是我想要做的。想知道是否有一种不那么明显的方式来做到这一点。但是,谢谢! – jpints14 2012-03-29 14:56:54
Dictionary<int, string> dictionary = new Dictionary<int, string>
{
{1,"One"},
{2,"Two"}
};
Hashtable hashtable = new Hashtable(dictionary);
试试这个
你可能要考虑使用Hashtable
构造函数重载接受一个IEqualityComparer
参数:
var hashtable = new Hashtable(dictionary, (IEqualityComparer) dictionary.Comparer);
这样,您的哈希表使用相同的Comparer
作为字典。例如,如果您的字典使用不区分大小写的字符串键,则您可能也希望Hashtable
也不区分大小写。例如:
var d = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
d.Add("a", "a");
d.Add("b", "b");
bool found;
found = d.ContainsKey("A"); // true
var hashtable1 = new Hashtable(d);
var hashtable2 = new Hashtable(d, (IEqualityComparer) d.Comparer);
found = hashtable1["A"] != null; // false - by default it's case-sensitive
found = hashtable2["A"] != null; // true - uses same comparer as the original dictionary
- 1. 如何将列表<object>转换为C#中的Hashtable?
- 2. 如何将对象转换为C#中的Dictionary <TKey,TValue>?
- 3. 如何将C#中的数据表转换为Dictionary <string,list <object>>?
- 4. C#将Dictionary <string,AnyType>转换为Dictionary <string,Object>(涉及反射)
- 5. Hashtable to Dictionary <> syncroot。
- 6. 如何将Dictionary <string,List <object>>转换成C#中的DataTable?
- 7. 如何将类转换为Dictionary <string,string>?
- 8. 如何将List <Class>转换成C#中的Dictionary <string,List <String>>?
- 9. Dictionary <string,string> vs Hashtable <string,string>
- 10. 如何将Dictionary <string,string>转换为LINQ中的属性字符串?
- 11. 如何将List <Dictionary <string,object >>转换为List <[具有动态属性的新类]>
- 12. 在C#中如何将ILIST <string>转换为ILIST <object>?
- 13. 如何在C++。net中将gcroot <Object^>转换为IMyInterface?
- 14. 如何将对象转换为列表<string>在C#中?
- 15. 无法将dynamicJsonObject转换为Dictionary <string,object>
- 16. 在Linq中将Dictionary <int,Dictionary <int, Foo>>转换为IEnumerable <IEnumerable <Foo>>的最简单方法是什么?
- 17. 如何将SortedList转换为SortedList <>
- 18. 如何将NameValueCollection转换为Hashtable
- 19. 如何使用System.Json将Dictionary <string,string>转换为json字符串?
- 20. 如何将List <BsonDocument>转换为C#中的<Class>列表
- 21. 如何将System.Collection.IEnumerable转换为c#中的列表<T>?
- 22. 如何将sortedDictionary转换为Dictionary?
- 23. 将转换列表<Cookie>转换为C#中的CookieCollection
- 24. 如何将List <IEnumerable <ProductViewModel >>转换为<IEnumerable <ProductViewModel >>?
- 25. 将Hashtable转换为System.Version
- 26. C#List <dictionary> To Json
- 27. C++将vector <pair <double,double >>转换为double *,double *?
- 28. C#将列表<string>转换为字典<string,string>
- 29. 将ICollection <Interface>转换为ICollection <Class> [UWP] c#6.0
- 30. 如何将列表<string>转换为列表<myEnumType>?
为什么你需要这个?哈希表是非常具体的,因为在关键字上计算出的值会产生该值的位置......并且字典只是一个直的(键,值)集合。你为什么要将字典转换为散列表? – 2012-03-29 14:50:42
在最简单的层面上,我很懒。 “Hashtable”类型的对象在很多地方使用。 – jpints14 2012-03-29 14:54:54
这本身并不是使用它的理由。字典和散列表是在不同情况下使用的不同的野兽。你知道,列表也用在很多地方......那么为什么不使用它呢? :-s –
2012-03-29 14:56:51