Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Two approaches immediately spring to mind:</p> <ol> <li><p>Do as Kevin suggested and write a struct that will serve as your key. Be sure to make this struct implement <code>IEquatable&lt;TKey&gt;</code> and to override its <code>Equals</code> and <code>GetHashCode</code> methods*.</p></li> <li><p>Write a class that utilizes nested dictionaries internally. Something like: <code>TripleKeyDictionary&lt;TKey1, TKey2, TKey3, TValue&gt;</code>... this class would internally have a member of type <code>Dictionary&lt;TKey1, Dictionary&lt;TKey2, Dictionary&lt;TKey3, TValue&gt;&gt;&gt;</code>, and would expose methods such as <code>this[TKey1 k1, TKey2 k2, TKey3 k3]</code>, <code>ContainsKeys(TKey1 k1, TKey2 k2, TKey3 k3)</code>, etc.</p></li> </ol> <p>*A word on whether overriding the <code>Equals</code> method is necessary: while it's true that the <code>Equals</code> method for a struct compares the value of each member by default, it does so by using reflection -- which inherently entails performance costs -- and is therefore <strong>not</strong> a very appropriate implementation for something that is meant to be used as a key in a dictionary (in my opinion, anyway). According to the MSDN documentation on <a href="http://msdn.microsoft.com/en-us/library/2dts52z7.aspx" rel="nofollow noreferrer"><code>ValueType.Equals</code></a>:</p> <blockquote> <p>The default implementation of the Equals method uses reflection to compare the corresponding fields of obj and this instance. Override the Equals method for a particular type to improve the performance of the method and more closely represent the concept of equality for the type.</p> </blockquote>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload