9

Does it have some special meaning when GetHashCode() function returns something using code which contains ^ symbol?

public class ClassProp
{
    public int Prop1 { get; set; }
    public int Prop2 { get; set; }
    public int Prop3 { get; set; }
    public int Prop4 { get; set; }
    public int Prop5 { get; set; }

    public override int GetHashCode()
    {
        return Prop1.GetHashCode() ^ Prop2.GetHashCode() ^ 
               Prop3.GetHashCode() ^ Prop4.GetHashCode() ^ Prop5.GetHashCode();
    }
}
Nilish
  • 1,066
  • 3
  • 12
  • 26
  • 1
    Many answers pointed that is't the XOR operator. It's used here just to create a combined hashcode. Take a look to this post here on SO: http://stackoverflow.com/questions/1079192/is-it-possible-to-combine-hash-codes-for-private-members-to-generate-a-new-hash – Adriano Repetti May 22 '12 at 16:10

5 Answers5

8

^ is the C# XOR operator. Nothing "special" about it, just that the hash codes of all the class properties are XOR'd together.

Edit: GetHashCode returns a generic code that is used as a shorthand identifier for a complex object. A common use is in hashing data structures when you want to store objects and then quickly retrieve them based on their hash code. Assume a class Person and some objects with the corresponding hash codes:

Alex 8540
John 9435
Peter 2453

These codes are generated based on some or all the fields of each object and must collide as rarely as possible to ensure efficient hashing. Now we can store the objects in a hash table using the hash code:

Entries
0 -> Alex
1 -> John
2 -> Peter

The objects are stored inside the table using their respective hash codes to determine the position. Next they can be easily retrieved by using the same hash code.

I suggest you find some literature about how hash tables work, because it's a bit too much to explain in an SO post.

Tudor
  • 61,523
  • 12
  • 102
  • 142
  • can you share a simple real example to understand when to use GetHashCode() function in real life project? – Nilish May 22 '12 at 16:19
  • DO you have a very basic article (a link) in your dictionary? – Nilish May 22 '12 at 16:39
  • @Kanav: You can start with the wikipedia page for a quick introduction: http://en.wikipedia.org/wiki/Hash_table – Tudor May 22 '12 at 16:46
  • can you explain it with a bug/issue, what exactly can be the problem for a class without `GetHashCode()` function? – Nilish May 22 '12 at 17:08
  • 1
    @Kanav these comments all constitute new questions. They are good questions and have quite possibly already been asked here. Your original question has already been answered. If you want answers to other questions then please ask new questions (after searching for pre-existing questions that serve your needs) – David Heffernan May 22 '12 at 17:16
4

That's just the bitwise xor operator. It is often used to combine hash codes from different objects into a single overall hash code.

It's not one of the easiest things to search for on Google! My tip when searching for such things is to look at the table of all operators.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • can you share a simple real example to understand when to use GetHashCode() function in real life project? – Nilish May 22 '12 at 16:18
  • You don't generally call `GetHashCode` much yourself. But the framework uses it for equality testing, dictionary hashing etc. Mostly you can get away with relying on the default implementation. – David Heffernan May 22 '12 at 16:20
  • Are you talking about this `public override bool Equals(Object obj) {}` ? If so, what's the relation between these two? – Nilish May 22 '12 at 16:22
  • Yes that's right. Please see this SO question: http://stackoverflow.com/questions/371328/why-is-it-important-to-override-gethashcode-when-equals-method-is-overriden-in-c – David Heffernan May 22 '12 at 16:25
  • can you explain it with a bug/issue, what exactly can be the problem for a class without `GetHashCode()` function? – Nilish May 22 '12 at 17:09
4

That's the bitwize XOR operator.

This is a very common operator used when implementing GetHashCode.

That being said, in this case, that implementation may not be ideal. The problem with using XOR (alone) is that you're not necessarily reducing the chance of collisions. The issue is that a class defined like so:

class Foo
{
    public int Bar { get; set; }
    public int Baz { get; set; }

    // ...
    public override int GetHashCode()
    {  return this.Bar.GetHashCode() ^ this.Baz.GetHashCode(); }
}

Is going to create the same hash code when Bar==2 and Baz==4 as when Bar==4 and Baz==2. Depending on the use case, this may lead to more hash collisions, so it is something to be aware of when implementing GetHashCode. Also - you should be very careful when you make a mutable type like this that your hash code implementation matches your equality checks, etc.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • 1
    can you share a simple real example to understand when to use GetHashCode() function in real life project? – Nilish May 22 '12 at 16:19
2

The bitwise XOR operator works as follows:

A = 10111 B = 01010

A ^ B = 11101

Different correspoding bits resutl in 1, similar ones result in 0.

In your case, those integers are being converted to binary first and then processed as in above example.

0

^ if the XOR operator in C# see here: http://msdn.microsoft.com/en-us/library/zkacc7k1.aspx

All your example is doing is XORing the hashcode from it's properties.

ilivewithian
  • 19,476
  • 19
  • 103
  • 165
  • can you share a simple real example to understand when to use GetHashCode() function in real life project? – Nilish May 22 '12 at 16:20