简体   繁体   中英

how to fetch keys of dictionary in C#

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.

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 KeyValuePair s in the dictionary too:

foreach(KeyValuePair<char, ulong> pair in dictionary)
{
    char c = pair.Key;
    ulong x = pair.Value;
    ...
}

字典提供了一个Keys属性,其中包含字典中的所有键。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM