Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# .NET GetHashCode function question

Hi I have a class with 6 string properties. A unique object will have different values for atleast one of these fields

To implement IEqualityComparer's GetHashCode function, I am concatenating all 6 properties and calling the GetHashCode on the resultant string.

I had the following doubts:

  1. Is it necessary to call the GetHashcode on a unique value?
  2. Will the concatenation operation on the six properties make the comparison slow?
  3. Should I use some other approach?
like image 994
ganeshran Avatar asked Sep 05 '11 14:09

ganeshran


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.

How old is the letter C?

The letter c was applied by French orthographists in the 12th century to represent the sound ts in English, and this sound developed into the simpler sibilant s.


2 Answers

If your string fields are named a-f and known not to be null, this is ReSharper's proposal for your GetHashCode()

public override int GetHashCode() {
  unchecked {
    int result=a.GetHashCode();
    result=(result*397)^b.GetHashCode();
    result=(result*397)^c.GetHashCode();
    result=(result*397)^d.GetHashCode();
    result=(result*397)^e.GetHashCode();
    result=(result*397)^f.GetHashCode();
    return result;
  }
}
like image 172
Corey Kosak Avatar answered Sep 23 '22 09:09

Corey Kosak


GetHashCode does not need to return unequal values for "unequal" objects. It only needs to return equal values for equal objects (it also must return the same value for the lifetime of the object).

This means that:

  1. If two objects compare as equal with Equals, then their GetHashCode must return the same value.
  2. If some of the 6 string properties are not strictly read-only, they cannot take part in the GetHashCode implementation.

If you cannot satisfy both points at the same time, you should re-evaluate your design because anything else will leave the door open for bugs.

Finally, you could probably make GetHashCode faster by calling GetHashCode on each of the 6 strings and then integrating all 6 results in one value using some bitwise operations.

like image 20
Jon Avatar answered Sep 25 '22 09:09

Jon