473,405 Members | 2,176 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Retrieving first key in Dictionary<T,T>

I have to retrieve the first kei I have inserted in a
Dictionary<int,intcalled dict.
The only method I found is:

Dictionary<int,int>.KeyCollection.Enumerator dke =
dict.Keys.GetEnumerator();
dke.MoveNext();
int first = dke.Current;

That seems a little convoluted, is there a more elegant way?
And, more importantly, can I be sure that this method retrieves the
first inserted key?

Note: I can't use SortedDictionary because I'm working on compact
framework.

Thanks

Mar 15 '07 #1
3 2159
Beorne wrote:
I have to retrieve the first kei I have inserted in a
Dictionary<int,int>
I don't think you can do this. Dictionary<,doesn't retain information
about the order of entry. Perhaps you could maintain a List<that
contains the keys as they are inserted. Or, better, build your own
collection class that does this internally.
--
Larry Lard
la*******@googlemail.com
The address is real, but unread - please reply to the group
For VB and C# questions - tell us which version
Mar 15 '07 #2
On Mar 15, 12:38 pm, Larry Lard <larryl...@googlemail.comwrote:
Beorne wrote:
I have to retrieve the first kei I have inserted in a
Dictionary<int,int>

I don't think you can do this. Dictionary<,doesn't retain information
about the order of entry. Perhaps you could maintain a List<that
contains the keys as they are inserted. Or, better, build your own
collection class that does this internally.

--
Larry Lard
larryl...@googlemail.com
The address is real, but unread - please reply to the group
For VB and C# questions - tell us which version
thanks!

Mar 15 '07 #3
Beorne,

If you were not using generics, I would suggest using the SortedList as in:

SortedList dd = new SortedList();
dd.Add(2, 4);
dd.Add(4, 7);

int answer = (int) dd.GetByIndex(0);
MessageBox.Show(this, "Value: " + answer);

The draw back here being boxing, but you do get your key value pairs and
index access that you wanted.

Dave
Mar 21 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: I am dog | last post by:
Hi , I use VS 2005 beta1 to do some thing as follows. declare a Dictionary<ulong,DelegateTyep> dictionary = new ... and use dictionary object such as dictionary += aDelegateTyepObject;
2
by: ESPNSTI | last post by:
Hi, I'm trying to use a generics dictionary with a key class that implements and needs IComparable<>. However when I attempt to use the dictionary, it doesn't appear to use the IComparable<> to...
8
by: Brian P | last post by:
I want to expose a property of Dictionary<string, MyAbstractClass>. I tried to do it this way: private Dictionary<string, MyChildClass> _dictionary; public Dictionary<string,...
12
by: Brett Romero | last post by:
If I want to take action on the Add event of a generic Dictionary, do I need to create a custom Dictionary and add an event handler for the Add() method? The dictionary is a public field on a...
5
by: Brian Richards | last post by:
I'm experiencing some wierd behavior with a Dictionary<T,U> class using foreach loops, such that the Key returned in the foreach is not contained in the dictionary. code: Dictionary<A, B>...
6
by: buzzweetman | last post by:
Many times I have a Dictionary<string, SomeTypeand need to get the list of keys out of it as a List<string>, to pass to a another method that expects a List<string>. I often do the following: ...
7
by: Andrew Robinson | last post by:
I have a method that needs to return either a Dictionary<k,vor a List<v> depending on input parameters and options to the method. 1. Is there any way to convert from a dictionary to a list...
2
by: Assimalyst | last post by:
Hi I have a Dictionary<string, List<string>>, which i have successfully filled. My problem is I need to create a filter expression using all possible permutations of its contents. i.e. the...
8
by: Peter Larsen [CPH] | last post by:
Hi, How do i concat two dictionaries aka the following sample: Dictionary<int, stringa; Dictionary<int, stringb; b.Add(a); What is the easiest way to concat two dictionaries ??
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.