F
Frank Rizzo
How do I get the first item that was added to the Dictionary object.
I've tried myDictionary[0], but it's not supported?
Regards
I've tried myDictionary[0], but it's not supported?
Regards
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
Frank said:How do I get the first item that was added to the Dictionary object.
I've tried myDictionary[0], but it's not supported?
How do I get the first item that was added to the Dictionary object.
I've tried myDictionary[0], but it's not supported?
Frank Rizzo said:How do I get the first item that was added to the Dictionary object.
I've tried myDictionary[0], but it's not supported?
No, it is not, as the dictionary doesn't maintain an order in which
the
elements are inserted. If you need to keep track of this, you should use
the OrderedDictionary class in the System.Collections.Specialized
namespace
(which is not generic)
Peter Duniho said:Can you confirm that the order of elements in an OrderedDictionary are in
fact maintained in the order in which they were added?
I couldn't find anything in the documentation for OrderedDictionary that
supports *or* refutes that. The closest I came was this statement: "The
elements of an OrderedDictionary are not sorted in any way". But that's
so vague that it doesn't, at least to me, actually say that you can't rely
on the order of the elements.
In other words, I see two possibilities:
* All that the "Ordered" means is that you can use an index to
retrieve elements from the collection, and that which element corresponds
to which index is undefined.
The ordering appears to be well defined. If you use only the Add
method, it
will be the order in which they were added, because
Add "Adds an entry with the specified key and value into the
OrderedDictionary collection with the lowest available index" and
RemoveAt "The entries that follow the removed entry move up to occupy the
vacated spot and the indexes of the entries that move are also updated."
While yes, the elements are not sorted by any attributes of the keys
or
values, they are sorted by position, which is implied by the index. If
the
numeric index didn't indicate position, then what would it represent?
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.