C#关联数组 [英] C# associative array
问题描述
我一直在使用Hashtable,但从本质上来说,哈希表不是有序的,我需要保持一切顺序,因为我添加他们(因为我想把它们以相同的顺序)。例如如果我这样做:
I've been using a Hashtable, but by nature, hashtables are not ordered, and I need to keep everything in order as I add them (because I want to pull them out in the same order). Forexample if I do:
pages["date"] = new FreeDateControl("Date:", false, true, false);
pages["plaintiff"] = new FreeTextboxControl("Primary Plaintiff:", true, true, false);
pages["loaned"] = new FreeTextboxControl("Amount Loaned:", true, true, false);
pages["witness"] = new FreeTextboxControl("EKFG Witness:", true, true, false);
当我做一个foreach时,我想能够按顺序得到它:
And when I do a foreach I want to be able to get it in the order of:
pages["date"]
pages["plaintiff"]
pages["loaned"]
pages["witness"]
我如何做?
推荐答案
我相信.NET有 OrderedDictionary
类来处理这个。它不是通用的,但它可以作为一个体面的Hashtable替代 - 如果你不关心严格的类型安全。
I believe that .NET has the OrderedDictionary
class to deal with this. It is not generic, but it can serve as a decent Hashtable substitute - if you don't care about strict type safety.
我写了一个通用的包装器类,我愿意分享。
I've written a generic wrapper around this class, which I would be willing to share.
http://msdn.microsoft.com/en-us/library/system.collections.specialized.ordereddictionary.aspx
这篇关于C#关联数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!