Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 任何现有词典<;T、 列表<;T>&燃气轮机;其中值集合不能包含键_C#_Dictionary - Fatal编程技术网

C# 任何现有词典<;T、 列表<;T>&燃气轮机;其中值集合不能包含键

C# 任何现有词典<;T、 列表<;T>&燃气轮机;其中值集合不能包含键,c#,dictionary,C#,Dictionary,我需要以下内容(为了简洁起见,这里使用字符串作为类型): MyDictionary:IDictionary { 私人只读词典收藏; ... //集合不能包含密钥 公共无效添加(字符串键、列表值) { _Add(key,newlist(value.RemoveAll(p=>p==key)); } } 因此,在使用中,您可能会: string myName = "Superstringcheese"; List<string> myFriends = new List<strin

我需要以下内容(为了简洁起见,这里使用字符串作为类型):

MyDictionary:IDictionary
{
私人只读词典收藏;
...
//集合不能包含密钥
公共无效添加(字符串键、列表值)
{
_Add(key,newlist(value.RemoveAll(p=>p==key));
}
}
因此,在使用中,您可能会:

string myName = "Superstringcheese";
List<string> myFriends = new List<string> {"John", "Jane", "Jerry"};

string yourName = "Someone";
List<string> yourFriends = new List<string> {"John", "Bill", "Ted"};

var myDic = new MyDictionary();

// It's okay that each of us (each key) has some friends that are the same. It's not okay for me to have myself as a friend. The add method would check if the key is contained by the value collection and remove it if necessary.
myDic.Add(myName, myFriends);
myDic.Add(yourName, yourFriends);
string myName=“Superstringcheese”;
List myFriends=新列表{“John”、“Jane”、“Jerry”};
string yourName=“Someone”;
列出你的朋友=新列表{“约翰”、“比尔”、“特德”};
var myDic=new MyDictionary();
//我们每个人(每个键)都有一些相同的朋友是可以的。我不可以将自己作为朋友。add方法将检查键是否包含在值集合中,并在必要时将其删除。
myDic.Add(我的名字,我的朋友);
myDic.Add(你的名字,你的朋友);

在我重新发明任何轮子之前,是否有一种本机集合类型可以实现这一点,或者有人知道一种流行的实现吗?

好的,我可以提供以下帮助:

是否存在执行此操作的本机集合类型


不。

好吧,我可以帮你做这件事:

是否存在执行此操作的本机集合类型


否。

那么您是在尝试为一个键定义一个具有多个值的字典吗?我曾经尝试过这样做,使用结构。我不明白为什么键会出现在值列表中?您能给我们展示一个使用Add方法的示例吗?当然,我添加了一个用法示例。@Superstringcheese:从您的示例中,我会说您的数据结构是“清单词典"。键不能在列表中的事实更多的是“业务规则”或“域规则”。数据结构和规则之间的区别有时是模糊的,不可否认。但无论如何,我认为您所描述的内容非常具体,因此如果有任何以前的实现值得重用,我会感到惊讶。我怀疑您在这方面是正确的,一个线索是我很难为它的广义形式想出一个名字。我会让它打开一点,看看有没有人有什么见解,有什么问题等等。所以你试图定义一个字典,一个键有多个值?我曾经尝试过这样做,使用一个结构。我不明白为什么键会出现在值列表中?你能给我们举个例子吗使用Add方法。当然,我添加了一个用法示例。@Superstringcheese:从您的示例中,我可以说您的数据结构是一个“列表字典”。键不能在列表中的事实更多的是“业务规则”或“域规则”。数据结构和规则之间的区别有时是模糊的,不可否认。但无论如何,我认为您所描述的内容非常具体,因此如果有任何以前的实现值得重用,我会感到惊讶。我怀疑您在这方面是正确的,一个线索是我很难为它的广义形式想出一个名字。我会让它打开一点,看看有没有人有什么见解,有什么问题等等。
string myName = "Superstringcheese";
List<string> myFriends = new List<string> {"John", "Jane", "Jerry"};

string yourName = "Someone";
List<string> yourFriends = new List<string> {"John", "Bill", "Ted"};

var myDic = new MyDictionary();

// It's okay that each of us (each key) has some friends that are the same. It's not okay for me to have myself as a friend. The add method would check if the key is contained by the value collection and remove it if necessary.
myDic.Add(myName, myFriends);
myDic.Add(yourName, yourFriends);