【问题标题】:How to remove element from Dictionary that uses Tuple如何从使用元组的字典中删除元素
【发布时间】:2015-12-30 16:30:35
【问题描述】:

我在我的 winform 上使用 C# 4.5。我有一个字典,其中包含一个名为 ParamList 的元组,它使用它:

new Dictionary<Tuple<string, string>, string>();

我能够遍历 ParamList 以将元素放置在我的 ComboBox 中。我想要做的是使用按钮单击事件向上或向下移动元素,或者使用不同的按钮单击完全删除元素。我不知道该怎么做。谢谢。

【问题讨论】:

  • A Tuple as key???? +_+
  • 您不能在字典中“移动”元素 - 它没有排序。删除一个元素很容易 - 但排序不是。
  • @kevintjuh93@ 是的,为什么不呢?这是完全合理的。
  • 我绝不会推荐这个,哈哈
  • 你说的是 ParamList 但你有字典 (?)

标签: c# dictionary elements


【解决方案1】:

使用元组删除

var dict = new Dictionary<Tuple<string, string>, string>();
dict.Add(new Tuple<string, string>("a", "b"), "c");
dict.Remove(new Tuple<string, string>("a", "b"));

System.Diagnostics.Debug.Assert(dict.Count == 0);

【讨论】:

  • 请注意,您通常应该使用 Tuple.Create 创建元组,因为它允许推断通用参数。这个答案甚至没有提到他想要重新订购物品的事实。
  • Eser,这适用于删除。在按钮单击事件上移动元素应该怎么做?
  • 我只是在我的 ComboBox 中移动了项目,这一切都处理好了。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-09
  • 2010-12-17
  • 1970-01-01
  • 1970-01-01
  • 2021-02-24
相关资源
最近更新 更多