Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何用c#创建具有两个键的字典?_C#_Dictionary - Fatal编程技术网

如何用c#创建具有两个键的字典?

如何用c#创建具有两个键的字典?,c#,dictionary,C#,Dictionary,在C#中,我希望有一个将(x,y)坐标映射到(x,y)的数据结构。我怎么能做这样的事 我不想使用像y*w+x这样的公式将x,y坐标转换为单个值。有没有办法让词典 如果我把键作为Tuple,那么它是一个对象,Tuple(1,1)不等于Tuple(1,1)。所以我认为我找不到那种意义上的键。var dict=new Dictionary(); var dict = new Dictionary<Point,string>(); dict[new Point(1,3)] = "asd";

在C#中,我希望有一个将(x,y)坐标映射到(x,y)的数据结构。我怎么能做这样的事

我不想使用像
y*w+x
这样的公式将x,y坐标转换为单个值。有没有办法让
词典

如果我把键作为Tuple,那么它是一个对象,Tuple(1,1)不等于Tuple(1,1)。所以我认为我找不到那种意义上的键。

var dict=new Dictionary();
var dict = new Dictionary<Point,string>();
dict[new Point(1,3)] = "asd";
dict[新点(1,3)]=“asd”;
您可以使用的任何类型的对象作为字典键,只要您正确覆盖该类型的
GetHashCode()
。这是字典将用来确定其中是否存在特定键的内容。因此,您可以创建自己的类并将其用作键


查看此答案以了解更多信息:

我之所以编写此答案,是因为其他答案似乎映射到1个字符串,而您需要映射到2个字符串。 您可以尝试使用点存储x和y位置,然后创建元组字典

var points = new Dictionary<Point,Tuple<string, string>>();
points[new Point(1,1)] = new Tuple<string, string>("2","2");
var points=newdictionary();
点[新点(1,1)]=新元组(“2”,“2”);
字典字典=新字典();
添加(“cat”,2);
字典。加上(“狗”,1);
添加(“骆驼”,0);
词典.添加(“鬣蜥”,-1);
if(dictionary.ContainsKey(“苹果”))
{
int值=字典[“苹果”];
控制台写入线(值);
}
难道你不能定义(或使用
系统.绘图
命名空间中的
为一个包含(x,y)的结构,然后在你的
字典
中使用它吗?

公共类点
{
公共字符串X{get;protected set;}
公共字符串Y{get;protected set;}
公共点(字符串x、字符串y)
{
X=X;
Y=Y;
}
公共HashSet GetSet()
{
HashSet result=新的HashSet();
结果。添加(此.X);
结果.添加(此.Y);
返回结果;
}
}
CreateSetComparer允许字典使用集合的值

List<Point> pointSource = GetSet();
Dictionary<HashSet<string>, Point> points = new Dictionary<HashSet<string>, Point>(HashSet<string>.CreateSetComparer());

foreach(Point d in pointSource)
{
    points.Add(d.GetSet(), d);
}
List pointSource=GetSet();
字典点=新字典(HashSet.CreateSetComparer());
foreach(点源中的点d)
{
添加(d.GetSet(),d);
}
找到一点:

HashSet<string> key = new HashSet<string>();
key.Add("X");
key.Add("Y");
Point myPoint = points[key];
HashSet key=newhashset();
添加(“X”);
键。添加(“Y”);
点myPoint=点[键];

如果您使用的是
struct
而不是
class
,则此链接可能是帮助完整的

,键将基于值进行比较,而不是基于参考,因为结构是值类型

public struct Point
{
   public int x;
   public int y;
   public Point(int x, int y)
   {
      this.x = x;
      this.y = y;
   }
 }
然后使用这个结构

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
var dic=newdictionary();
增加(新的第(1,1)点,新的第(1,2)点);
var f=dic[新点(1,1)];
控制台写入线(f.x)//输出将为1

你不能定义(或使用预先存在的)
为一个包含
(x,y)
的结构,然后在你的
字典中使用它吗?
?作为答案添加,所以你可以接受它:-)@omega记住如果你使用System.Drawing.Point,那么你的点坐标应该是
int
public struct Point
{
   public int x;
   public int y;
   public Point(int x, int y)
   {
      this.x = x;
      this.y = y;
   }
 }
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