GetHashCode () with ^

Is there any special meaning when a function GetHashCode()returns something using code that 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();
    }
}
+5
source share
5 answers

This is just a bitwise xor operator . It is often used to combine hash codes from different objects into a single common hash code.

This is not one of the easiest things to search on Google! My advice when looking for such things is to look at the table of all operators .

+4
source

^- C # XOR operator . There is nothing special about this, just the hash codes of all the properties of the XOR'd class together.

: GetHashCode , . , , -. , Person -:

Alex 8540
John 9435
Peter 2453

, . - -:

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

- . , -.

, -, , SO.

+4

XOR.

, GetHashCode.

. XOR () , . , :

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

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

-, Bar == 2 Baz == 4, Bar == 4 Baz == 2. -, GetHashCode. , , , - ..

+2

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

All you do is XORing hashcode from its properties.

0
source

The XOR bitwise operator works as follows:

A = 10111 B = 01010

A ^ B = 11101

Different resutl correlation bits in 1, similar - 0.

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

0
source

All Articles