How do I get the list of keys in a Dictionary?

后端 未结 9 864
梦谈多话
梦谈多话 2020-12-02 11:38

I only want the Keys and not the Values of a Dictionary.

I haven\'t been able to get any code to do this yet. Using another array proved to be too much work as I use

9条回答
  •  广开言路
    2020-12-02 12:27

    The question is a little tricky to understand but I'm guessing that the problem is that you're trying to remove elements from the Dictionary while you iterate over the keys. I think in that case you have no choice but to use a second array.

    ArrayList lList = new ArrayList(lDict.Keys);
    foreach (object lKey in lList)
    {
      if ()
      {
        lDict.Remove(lKey);
      }
    }
    

    If you can use generic lists and dictionaries instead of an ArrayList then I would, however the above should just work.

提交回复
热议问题