从词典中的词典中删除项目 [英] Removing an item from a Dictionary within a Dictionary

查看:100
本文介绍了从词典中的词典中删除项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一本字典

<br />
Dictionary<int, Dictionary<int, String>><br />


从嵌套"字典中删除项目的最佳方法是什么?

例如:


What is the best way to remove an item from the "nested" dictionary?

For e.g.:

Dictionary<int, Dictionary<int, string>> outer = new Dictionary<int, Dictionary<int, string>>();


然后,该词典中填充了一些东西,我想删除现有的项目 outer [1] [2] ,我通常会这样做:


This Dictionary is then populated with stuff and I want to remove the existing item outer[1][2], I would usually do:

Dictionary<int, string> inner = outer[1];
outer.Remove(1);
inner.Remove(2);
outer.Add(1, inner);



有没有更好/更短/更直接的方法?



Is there a better/shorter/more straightforward way?

推荐答案

从其中删除项目时无需删除并重新添加内部字典,因此您可以做:
There is no need to remove and re-add the inner dictionary when removing an item from it, so you can just do:
// To remove outer[1][2]
outer[1].Remove(2);


这篇关于从词典中的词典中删除项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆