omega
omega

Reputation: 43913

How to create dictionary in c# with two keys?

In C#, I want to have a data structure that maps (x,y) coordinates to (x,y). How can I do something like this?

I don't want to convert the x,y coordinate into a single value using a formula like y*w+x. Is there a way to have dictionary<key,key,(value,value)>.

If I put the key as Tuple, then its an object and Tuple(1,1) does not equal Tuple(1,1). So I don't think I can find keys in that sense.

Upvotes: 3

Views: 3966

Answers (7)

marc_s
marc_s

Reputation: 755321

Can't you just define (or use the pre-existing structure from the System.Drawing namespace) Point as a structure that holds (x, y) and then use that in your Dictionary<Point, Point>?

Upvotes: 3

Taher  Rahgooy
Taher Rahgooy

Reputation: 6696

If you use struct rather than class the keys will be compared based on the values not based on the reference, because the structs are value type

public struct Point
{
   public int x;
   public int y;
   public Point(int x, int y)
   {
      this.x = x;
      this.y = y;
   }
 }

Then using this struct

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

Upvotes: 6

Krunal Mevada
Krunal Mevada

Reputation: 1655

public class Point
{
    public string X {get; protected set;}
    public string Y {get; protected set;}
    public Point(string x, string y)
    {
        X = x;
        Y = y;
    }

    public HashSet<string> GetSet()
    {
        HashSet<string> result = new HashSet<string>();
        result.Add(this.X);
        result.Add(this.Y);
        return result;
    }
}

CreateSetComparer allows the dictionary to use the values of the set

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);
}

And finding a point:

HashSet<string> key = new HashSet<string>();
key.Add("X");
key.Add("Y");
Point myPoint = points[key];

May this link is help full HashCode

Upvotes: 0

Minu  Manjaly
Minu Manjaly

Reputation: 51

Dictionary<string, int> dictionary =  new Dictionary<string, int>();

dictionary.Add("cat", 2);
dictionary.Add("dog", 1);
dictionary.Add("llama", 0);
dictionary.Add("iguana", -1);


if (dictionary.ContainsKey("apple"))
{
    int value = dictionary["apple"];
    Console.WriteLine(value);
}

Upvotes: -1

Shane
Shane

Reputation: 336

I'm writing this answer because the other ones seem to map to 1 string, and you need to map to 2 strings. You could try using Point to stores the x and y position and then create a dictionary of a Tuple.

var points = new Dictionary<Point,Tuple<string, string>>();
points[new Point(1,1)] = new Tuple<string, string>("2","2");

Upvotes: 1

Ioannis Karadimas
Ioannis Karadimas

Reputation: 7906

You can use any kind of object for as a dictionary key, as long as you correctly override GetHashCode() for that type. This is what the dictionary will use to determine whether a specific key exists in it or not. Therefore you can create your own class and use that as a key.

Check this answer for more information: What is the best algorithm for an overridden System.Object.GetHashCode?

Upvotes: 1

Backs
Backs

Reputation: 24913

var dict = new Dictionary<Point,string>();
dict[new Point(1,3)] = "asd";

Upvotes: 1

Related Questions