By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,225 Members | 2,223 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,225 IT Pros & Developers. It's quick & easy.

How to get the first item in Dictionary?

P: n/a
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
Jun 4 '07 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Frank,

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) or the KeyedCollection class in the
System.Collections.ObjectModel namespace (which is generic).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Frank Rizzo" <no**@none.comwrote in message
news:e4**************@TK2MSFTNGP05.phx.gbl...
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

Jun 4 '07 #2

P: n/a
Frank Rizzo wrote:
How do I get the first item that was added to the Dictionary object.
I've tried myDictionary[0], but it's not supported?
That's not possible using the generic implementation as the underlying
storage is a hashtable and the order in which items are returned is
undefined. You get an item based on key, not based on the order that it
was added.
--
Tom Porterfield
Jun 4 '07 #3

P: n/a
On Mon, 04 Jun 2007 09:07:56 -0700, Frank Rizzo <no**@none.comwrote:
How do I get the first item that was added to the Dictionary object.
I've tried myDictionary[0], but it's not supported?
I'm not sure you can. Since Dictionary is a hashtable, items are not
stored in order of addition to the Dictionary.

You could try using foreach() and just grabbing the first item that is
returned, or using Dictionary.Values[0]. But I suspect both of those will
just return the item with the lowest-valued hash code. It probably won't
necessarily be the first one you added (and in fact, probably won't be
most of the time).

If you need to preserve order in your collection, I'd recommend using the
SortedDictionary<generic class instead, where the key you use is some
monotonically increasing value (DateTime.Now, or just a simple integer
counter, for example). In that case, the collection *does* have order,
and it makes sense to ask it for the "first" element.

Pete
Jun 4 '07 #4

P: n/a
Frank Rizzo <no**@none.comwrote:
How do I get the first item that was added to the Dictionary object.
I've tried myDictionary[0], but it's not supported?
As others have pointed out, dictionaries are inherently unsorted.
Basically, you need to keep a List as well.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jun 4 '07 #5

P: n/a
On Mon, 04 Jun 2007 09:31:37 -0700, Nicholas Paldino [.NET/C# MVP]
<mv*@spam.guard.caspershouse.comwrote:
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)
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.

* "Ordered" actually means that there *is* a defined order to the
collection and that this order, while not sorted, does correspond exactly
to the order in which items were added.

I haven't found anything in the documentation that would help resolve the
ambiguity. Do you have information that does? A link to a documentation
page would be especially helpful.

Thanks,
Pete
Jun 4 '07 #6

P: n/a
Peter,

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?

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
On Mon, 04 Jun 2007 09:31:37 -0700, Nicholas Paldino [.NET/C# MVP]
<mv*@spam.guard.caspershouse.comwrote:
> 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)

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.

* "Ordered" actually means that there *is* a defined order to the
collection and that this order, while not sorted, does correspond exactly
to the order in which items were added.

I haven't found anything in the documentation that would help resolve the
ambiguity. Do you have information that does? A link to a documentation
page would be especially helpful.

Thanks,
Pete

Jun 4 '07 #7

P: n/a
Also, the RemoveAt and Insert methods show that the numeric index is
infact a positional index. Granted, the order can be changed, but for any
other collection I have seen, adding a new item always adds it to the end of
the list.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
On Mon, 04 Jun 2007 09:31:37 -0700, Nicholas Paldino [.NET/C# MVP]
<mv*@spam.guard.caspershouse.comwrote:
> 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)

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.

* "Ordered" actually means that there *is* a defined order to the
collection and that this order, while not sorted, does correspond exactly
to the order in which items were added.

I haven't found anything in the documentation that would help resolve the
ambiguity. Do you have information that does? A link to a documentation
page would be especially helpful.

Thanks,
Pete

Jun 4 '07 #8

P: n/a

"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
On Mon, 04 Jun 2007 09:31:37 -0700, Nicholas Paldino [.NET/C# MVP]
<mv*@spam.guard.caspershouse.comwrote:
> 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)

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

You can control the order more precisely using Insert.
* "Ordered" actually means that there *is* a defined order to the
collection and that this order, while not sorted, does correspond exactly
to the order in which items were added.

I haven't found anything in the documentation that would help resolve the
ambiguity. Do you have information that does? A link to a documentation
page would be especially helpful.

Thanks,
Pete

Jun 4 '07 #9

P: n/a
On Mon, 04 Jun 2007 13:17:13 -0700, Ben Voigt [C++ MVP]
<rb*@nospam.nospamwrote:
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."
Thank you...that was the sort of thing I was looking for in the docs, but
didn't manage to find the right page. Much obliged.
Jun 4 '07 #10

P: n/a
On Mon, 04 Jun 2007 13:18:04 -0700, Nicholas Paldino [.NET/C# MVP]
<mv*@spam.guard.caspershouse.comwrote:
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?
Well, I thought my post was clear enough on that. However, as an example
of the ambiguity, it could be that accessing an item via an index simply
returns items in the same order that foreach() does. All collections that
I've seen implement IEnumerable and so allow foreach(), but not all
collections mandate a specific order for items to be retrieved via
foreach().

In any case, while I am surprised that the reference for the Item property
doesn't make this clear, I agree that the reference for Add() and
RemoveAt() do. Thanks to you both for your replies.

Pete
Jun 4 '07 #11

This discussion thread is closed

Replies have been disabled for this discussion.