任何现有的Dictionary <t,list <t =“”>&gt;其中值集合不能包含密钥</t,>

时间:2012-04-23 14:28:36

标签: c# dictionary

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

MyDictionary : IDictionary<string, List<string>>
{
    private readonly Dictionary<string, List<string>> _collection;
    ...
    // The collection cannot contain the key
    public void Add(string key, List<string> value)
    {
        _collection.Add(key, new List<string>(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);

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

1 个答案:

答案 0 :(得分:1)

嗯,我可以帮忙解决这个问题:

  

是否有本地集合类型可以执行此操作?

没有