What Dotnet Collection Class's Items Can Be Enumerated In "addition Order" And Retrieved Via A Key?
I'm lead to believe that I cannot count on the order of items added to a dictionary for enumeration purposes.
Is there a class (generic if possible) to which items may be added with a key and which can be enumerated in addition order or which can be retrieved by key?
Clarification: I do not want to enumerate in Key Order. I want to enumerate in addition order. Which is to say that I want to be able to retrieve the items via enumeration on a FIFO ( first in first out) basis.
First, on your primary assumption, you are correct. A normal dictionary makes no guarantees about the order of enumeration.
Second, you'll need to be careful about going the
SortedDictionary with custom
IComparer route. The comparer is used for key equality as well as sorting the collection. That is, using an
IComparer based on the addition order, you may have difficulty retrieving an element from a
SortedDictionary by key value, it might end up lost in the tree (which is the backing of a sorted dictionary).
If you're willing to go the C5 Generic Class Library route, you can get some good mileage out of a
HashedLinkedList<T> if T is self-keyed. You can create an
IEqualityComparer that will operate on the key to generate the hash code. Then retrieving the actual value, you can use
Find(ref T x) with a prototype x (perhaps where only the key is set) which will find the stored
T and return that by reference in O(1) time vs. O(log n) with a
SortedDictionary. As well, being backed by a
LinkedList, it is guaranteed to enumerate in addition order (and you can specify which direction you'd prefer through C5's
Hope that helps.
- → Function Undefined in Axios promise
- → React formatting dates with momentjs server side
- → Using ReactJs, How do I update my form fields when my parent object sent in from props changes?
- → Visual Studio 2012 Express: Browser returning 500 status trying to download jsx file
- → AngularJS directive: "templateUrl" doesn't work while "template" works
- → how to add cannonical tag for ASPX .NET page
- → Selecting an element by its attribute when it has a colon in its name
- → Function works for NON dynamically loaded images
- → URL routing requires /Home/Page?page=1 instead of /Home/Page/1
- → There are "gaps" between rows in my masonry cards
- → creating sef links and changing plus sign (+) with dash (-)