Remove from Dictionary by Key and Retrieve Value

后端 未结 6 483
广开言路
广开言路 2020-12-17 20:05

Is there a way to remove an entry from a Dictionary (by Key) AND retrieve it\'s Value in \"the same step?\"

For example, I\'m

6条回答
  •  [愿得一人]
    2020-12-17 20:48

    You can extend the class to add that functionality:

    public class PoppableDictionary : Dictionary
    {
        public V Pop(T key)
        {
            V value = this[key];
            this.Remove(key);
            return value;
        }
    }
    

提交回复
热议问题