.NET dictionary with two keys and one value

前端 未结 13 3024
盖世英雄少女心
盖世英雄少女心 2020-12-14 00:23

Is there a dictionary available in .NET that could hold 2 keys and one value. Like

Dictionary(Of TKey, Of TKey, TValue)

I have

13条回答
  •  难免孤独
    2020-12-14 00:37

    interesting question, here's one solution. You have to add an indexer for every key type you want to support though.

    public class NewDic
    {
        public void Add(string key1, long key2, T value)
        {
            mDic.Add(key1, value);
            mDic.Add(key2, value);
        }
    
        public T this[string s]
        {
            get { return mDic[s]; }
        }
    
        public T this[long l]
        {
            get { return mDic[l]; }
        }
    
    
        Dictionary mDic = new Dictionary();
    }
    
            NewDic dic = new NewDic();
    
            dic.Add("abc", 20, 10);
    
            Console.WriteLine(dic["abc"]);
            Console.WriteLine(dic[20]);
    

提交回复
热议问题