未排序的哈希表 [英] unsort Hashtable
问题描述
我正在用C#编写程序 我有这样的代码
i am writing a program in C# i have a code like this
Hashtable ht = new Hashtable();
ht.Add("1", "One");
ht.Add("2", "Two");
ht.Add("3", "Three");
ht.Add("4", "Four");
但是编译器对它进行排序 我想知道如何防止对哈希表进行排序 请帮助我
but Compiler sort it i wanna know how can i prevent sorting hashtable please help me
推荐答案
HashTable
不会进行此类排序.它会根据项目的哈希码进行重新排列,因此不会保留原始顺序.
A HashTable
doesn't do sorting as such. It does rearrange the items based on their hash code, so the original order isn't preserved.
如果要保留原始顺序或要指定排序顺序,则不能仅使用HashTable
.
If you want to preserve the original order, or want to specify a sort order, you can't use a HashTable
only.
要指定其他排序顺序,可以使用SortedDictionary<T>
.要保留原始顺序,可以将项目同时添加到Dictionary<T>
和List<T>
.
To specify a different sort order, you can use a SortedDictionary<T>
. To preserve the original order, you can add the items both to a Dictionary<T>
and a List<T>
.
这篇关于未排序的哈希表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!