C# GetHashCode

In this chapter you will learn:

  1. Override the GetHashCode method from object
  2. Example for GetHashCode Method

Hash code

Hash code is used by C# to distinguish one object from another. If two object represent two difference value they should have different hash code value.

Example

Example for GetHashCode Method


using System;//  w  w w.  jav a2 s.  c o m

public class ComplexNumber
{
    public ComplexNumber( double real, double imaginary ) {
        this.real = real;
        this.imaginary = imaginary;
    }

    public override int GetHashCode() {
        return (int) Math.Sqrt( Math.Pow(this.real, 2) * Math.Pow(this.imaginary, 2) );
    }

    private readonly double real;
    private readonly double imaginary;
}

Next chapter...

What you will learn in the next chapter:

  1. What is Object Casting and Reference Conversions
  2. Upcasting
  3. Downcasting
Home »
  C# Tutorial »
    C# Types »
      C# Object
C# Object
C# new operator
C# this Reference
C# Null
C# Nullable Types
C# ToString Method
C# Object Initializers
C# GetHashCode
C# Object Casting and Reference Conversions