Hashtable with MultiDimensional Key in C#

后端 未结 16 1545
闹比i
闹比i 2020-11-27 02:58

I\'m basically looking for a way to access a hashtable value using a two-dimensional typed key in c#.

Eventually I would be able to do something like this



        
16条回答
  •  不知归路
    2020-11-27 03:29

    How about using a regular Dictionary with some kind of Tuple structure as a key?

    public class TwoKeyDictionary
    {
        private readonly Dictionary, V> _dict;
    
        public V this[K1 k1, K2 k2]
        {
            get { return _dict[new Pair(k1,k2)]; }
        }
    
        private struct Pair
        {
            public K1 First;
            public K2 Second;
    
            public override Int32 GetHashCode()
            {
                return First.GetHashCode() ^ Second.GetHashCode();
            }
    
            // ... Equals, ctor, etc...
        }
    }
    

提交回复
热议问题