tags:

views:

123

answers:

2

I'm using a Dictionary<char, ulong> where the char is a obj no, & ulong is offset.

Now, I need to access the Keys of Dictionary. Keys is any random number which cant predict. I am using VS-2005. I am new to C# so plz provide some code.

+3  A: 

Dictionary provides a Keys property that contains all of the keys in the dictionary.

Matt Breckon
+5  A: 

As Matt says, the Keys property is your friend. This returns a KeyCollection, but usually you just want to iterate over it:

foreach (char key in dictionary.Keys)
{
    // Whatever
}

Note that the order in which the keys are returned is not guaranteed. In many cases it will actually be insertion order, but you must not rely on that.

I'm slightly concerned that you talk about the keys being random numbers when it looks like they're characters. Have you definitely chosen the right types here?

One more tip - if you will sometimes need the values as well as the keys, you can iterate over the KeyValuePairs in the dictionary too:

foreach(KeyValuePair<char, ulong> pair in dictionary)
{
    char c = pair.Key;
    ulong x = pair.Value;
    ...
}
Jon Skeet
shouldn't that be foreach(char key in dictionary.Keys), Jon?
Shoko
so this is why you and Marc have got such a high rep! Nice answers - if I update mine now it'll just look like I'm copying.
Matt Breckon
Was that edited? I'm *sure* the "tip" wasn't there when I added a comment...
Marc Gravell
@Marc: Yes, I edited a couple of times. Did you answer as well? I didn't see it. @Shoko: Yes, I fixed that in the first edit. I was remembering the "number" part of the post, which was why I assumed it was ulong as the key. Then when I posted it, I spotted the mistake and edited.
Jon Skeet