Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# use a class object as a key in dictionary

Tags:

c#

I have a class that looks like this.

public class Point : IEquatable<Point>
{
    public int _xValue {get;set;}
    public double _yValue {get;set;}

    public Point(int x, double y)
    {
        _xValue = x;
        _yValue = y;
    }

    public override bool Equals(object obj)
    {
        return Equals(obj as Point);
    }

    public bool Equals(Point obj)
    {
        return obj != null && obj._xValue == this._xValue && obj._yValue == this._yValue;
    }


}

I have to implement the GetHashCode function so that I can use it as a dictionary key. But I'm not clear on what the GetHashCode function has to return in this situation. Can someone help me out?

like image 394
Aks Avatar asked Feb 03 '11 05:02

Aks


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

Why do we write C?

We write C for Carbon Because in some element the symbol of the element is taken form its first words and Co for Cobalt beacause in some elements the symbol of the element is taken from its first second letters, so that the we don't get confuse.


3 Answers

It should return an integer, preferably unique for each seperate instance of the object. A hash value is basically a single number created out of the contents of an object, used to uniquely identify that object. The number one rule is that if two of those Points evaluate as equal to each other, the hash value should be the same for both of them.

A more detailed description is available at MSDN

like image 195
Xono Avatar answered Sep 23 '22 06:09

Xono


The GetHashCode function needs to return am integer that will uniquely identify one instance of the object from another so as to avoid collisions when it is used as a key in the dictionary.

You should be able to reliably reproduce the hash code so try and avoid using random or date values as seeds for the hash code.

like image 27
lomaxx Avatar answered Sep 22 '22 06:09

lomaxx


You could do _xValue ^ _yValue.GetHashCode()

like image 20
leeny Avatar answered Sep 22 '22 06:09

leeny