如何使用 3 维键创建字典

Dan*_*iel 4 c# dictionary valuetuple

我需要某种像字典一样工作的结构,但关键是一个 3D 点(由 3 个整数表示)。

Ball b1 = new Ball();
Ball b2 = new Ball();

D[-1,3,29] = b1;
D[9,0,3] = b2;
Run Code Online (Sandbox Code Playgroud)

我正在考虑创建这样的东西:

Dictionary<int, Dictionary<int, Dictionary<int, Ball>>> D = new ...
Run Code Online (Sandbox Code Playgroud)

有没有更好的方法?

AAA*_*ddd 13

AValueTuple与简单类型一起使用时具有适当的相等性和哈希码行为。因此,您可以在字典中使用它们(并且是合适的)。

var dict = new Dictionary<(int,int,int),something>
dict.Add((1,2,3), sometthing);
Run Code Online (Sandbox Code Playgroud)