在 C# 中,我想要一个将 (x,y) 坐标映射到 (x,y) 的数据结构。我怎样才能做这样的事情?
我不想使用类似 的公式将 x,y 坐标转换为单个值y*w+x。有没有办法可以拥有dictionary<key,key,(value,value)>.
如果我将键作为 Tuple,那么它是一个对象,并且 Tuple(1,1) 不等于 Tuple(1,1)。所以我认为我无法找到这个意义上的钥匙。
如果您使用struct而不是class键,则将根据值而不是引用进行比较,因为结构是值类型
public struct Point
{
public int x;
public int y;
public Point(int x, int y)
{
this.x = x;
this.y = y;
}
}
Run Code Online (Sandbox Code Playgroud)
然后使用这个结构体
var dic = new Dictionary<Point,Point>();
dic.Add(new Point(1,1), new Point(1,2));
var f = dic[new Point(1,1)];
Console.WriteLine(f.x); //Output will be 1
Run Code Online (Sandbox Code Playgroud)