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

后端 未结 9 817
梦谈多话
梦谈多话 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 (<your condition here>)
      {
        lDict.Remove(lKey);
      }
    }
    

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

    0 讨论(0)
  • 2020-12-02 12:27

    I often used this to get the key and value inside a dictionary: (VB.Net)

     For Each kv As KeyValuePair(Of String, Integer) In layerList
    
     Next
    

    (layerList is of type Dictionary(Of String, Integer))

    0 讨论(0)
  • 2020-12-02 12:32
    List<string> keyList = new List<string>(this.yourDictionary.Keys);
    
    0 讨论(0)
提交回复
热议问题