是否可以在 C# 的字典中使用具有不同值的重复键?

问题描述 投票:0回答:0

我有像下面这样的

Dictionary

关键是杯子、笔、胶带、眼镜、石头之类的名字。这些值将是包含数量、类型、图像等内容的类

值来自 JSON 文件,包含所有具有与值关联的键的值。

是否可以有不同值的重复键?

例如,我想为每个键添加新值,并且具有重复键的不同值。

我想得到这样的输出

name: cup
amount: 3
colors: red
location: home //this will be the new value
name: cup
amount: 3
colors: yellow //duplicated key cup with different value
location: home //this will be the new value

新值将是位置:

地点:家

可能吗?

Dictionary<string, Things> myDictionary;
public class Things{
    public string name;
    public int amount;
    public string types;
    //...
}

foreach(Things item in thingsList.jsonfile)
{
  //go through each item and add to item key as name and other for values
}

JSON

  "Things": [
      {
        "name": "cups",
        "amount": 5,
        "colors": "red",
        //...
      },
      {
        "name": "pens",
        "amount": 3,
        "colors": "blue",
      },
      {
        "name": "cup",
        "amount": 3,
        "colors": "yellow",
      }
    ],

我看了这个并且可以使用

Lookup
但不确定如何使用它,因为我无法找到使用
Lookup
Dictionary
的例子。

.NET 词典中的重复键?

c# dictionary duplicates key lookup
© www.soinside.com 2019 - 2024. All rights reserved.