开发者

C# associative array

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 FreeText开发者_如何学GoboxControl("EKFG Witness:", true, true, false);

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"] 

How can I do this?


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


EDIT: LBushkin is right - OrderedDictionary looks like it does the trick, albeit in a non-generic way. It's funny how many specialized collections there are which don't have generic equivalents :( (It would make sense for Malfist to change the accepted answer to LBushkin's.)

(I thought that...) .NET doesn't have anything built-in to do this.

Basically you'll need to keep a List<string> as well as a Dictionary<string,FreeTextboxControl>. When you add to the dictionary, add the key to the list. Then you can iterate through the list and find the keys in insertion order. You'll need to be careful when you remove or replace items though.


use sorted list i think it will solve your problem becuase SortedList object internally maintains two arrays to store the elements of the list; that is, one array for the keys and another array for the associated values. Each element is a key/value pair that can be accessed as a DictionaryEntry object

SortedList sl = new SortedList();

foreach(DictionaryEntry x in sl) {}


Use the KeyedCollection

Its underlying base is a List but provides a dictionary lookup based on key. In this case your key is the strings. So as long as you aren't adding the same key twice you are fine.

http://msdn.microsoft.com/en-us/library/ms132438.aspx


There's no perfect solution before .NET 4.0. In < 3.5 You can:

Use a generic SortedList with integer key-type, and value type of the most-derived common type of your items. Define an integer value (i, let's say) and as you add each item to the SortedList, make the key i++, incrementing it's value as you go. Later, iterate over the GetValueList property of the sorted list. This IList property will yield your objects in the order you put them in, because they will be sorted by the key you used.

This is not lightening-fast, but pretty good, and generic. If you want to also access by key, you need to do something else, but I don't see that in your requirements. If you don't new to retrieve by key, and you add items in key order so the collection doesn't actually have to do its sorting, this is it.

In .NET 4.0 you'll have the generic SortedSet Of T, which will be absolutely perfect for you. No tradeoffs.


The best way is to use the C# indexers. It is configurable to anything we like. We can pass an int, enum, long, double or anything we like.

Just have to create a class and give it indexers and configure input and output parameters. It is a little more work but I think this is the only right way.

Please see this MSDN link for more information how to use it.


See Indexers: http://msdn.microsoft.com/en-us/library/6x16t2tx.aspx


One alternative is to keep your ordered key values in an ordered structure like a List, the rest being still stored in a dictionnary.

Then, when you need to access your data, just go through your sorted List and query your dictionnary along the way.


look at sorted list http://msdn.microsoft.com/en-us/library/system.collections.sortedlist.aspx


As Haxelit suggests, you might derive from KeyedCollection<TKey, TValue>. It actually uses a List underneath until you hit a certain threshold value, and then it maintains both a List and a Dictionary. If you can use a function to derive one of your keys from one of your values, then this is an easy solution. If not, then it gets pretty messy.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜